2

I have three dates A, B and C.

A = 2013-08-10 10:00
B = 2013-08-10 12:00
C = 2013-08-10 10:22

What I am trying to do is check if C is inside A and B, if it is return true. Anyone have any idea of how to do this?

I tried this with no luck

    if ($time >= $date_start && $time <= $date_end)
    {
        echo "is between\n";
    } else {
        echo 'no';
    }
4

4 回答 4

11

您可以将它们转换为 UNIX 时间戳进行比较。

$A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
$B = strtotime($B);
$C = strtotime($C);

if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
  echo "Yes '$C' is between '$A' and '$B'";
 }
于 2013-08-13T12:54:54.117 回答
4

使用以下代码比较 php 中的日期值

$a = new DateTime("2013-08-10 10:00");
$b = new DateTime("2013-08-10 12:00");
$c = new DateTime("2013-08-10 10:22");

if ($a < $c && $c < $b ) {
    return true;
}
于 2013-08-13T13:03:38.140 回答
3

使用 strtotime 函数。

$A = "2013-08-10 10:00";
$B = "2013-08-10 12:00";
$C = "2013-08-10 10:22";

if (strtotime($C) > strtotime($A) && strtotime($C) < strtotime($B)){
    echo "The time is between time A and B.";
} else {
    echo "It is not between time A and B.";
}
于 2013-08-13T12:52:10.357 回答
-1

使用DateTime类:

$A = '2013-08-10 10:00';
$B = '2013-08-10 12:00';
$C = '2013-08-10 10:22';

$dateA = DateTime::createFromFormat('Y-m-d H:m', $A);
$dateB = DateTime::createFromFormat('Y-m-d H:m', $B);
$dateC = DateTime::createFromFormat('Y-m-d H:m', $C);

if ($dateA >= $dateB && $dateA <= $dateC)
{
  echo "$dateA is between $dateB and $dateC";
}
于 2013-08-13T12:57:38.557 回答