从Datetime大于的数组中取消设置变量


Unset Variables from array where Datetime is greater than

所以我一直在尝试解决这个问题一段时间,问题是我有一个数组

$time=array("10:00 AM","11:00 AM","12:00 PM","1:00 PM","2:00 PM","3:00 PM","4:00 PM","5:00 PM");

问题是,如果现在的时间比数组中的值大,我试图去掉数组中的变量,然后从数组中去掉值,否则就把它留在那里,然后转到下一个位置

这是我迄今为止尝试过的

$tz_object = new DateTimeZone('America/New_York');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
$datetime->format('g:i A');
$i=0;
while($i<= count($time)){
    if ($datetime> '12:00 PM') {
      echo "take off time from the array";
    }
    $i++;
}

我应该在我的阵列中得到这样的结果

$time=array("1:00 PM","2:00 PM","3:00 PM","4:00 PM","5:00 PM");

您需要实际检索格式化的日期。

$current = $datetime->format('g:i A');

然后在循环中,您可以使用strtotime将每个转换为一个整数:

if ( strtotime($current) > strtotime($time[$i]) ) {
    unset($time[$i]);
} 
<?php
$time=array("10:00 AM","11:00 AM","12:00 PM","1:00 PM","2:00 PM","3:00 PM","4:00 PM","5:00 PM");
$tz_object = new DateTimeZone('America/New_York');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);

//$now = "12:01";
$now = $datetime->format('H:i');

$i=0;
while($i< count($time)){
     $compare = date("H:i", strtotime($time[$i] ));
    if ($now > $compare ) {
      echo "take off time from the array<br />";
      unset($time[$i]);   
    }
    else {
     echo $now ." < ". $compare . "<br />";
    }
    $i++;
}
print_r($time);