比较给定date和今天
我有以下
$var = "2010-01-21 00:00:00.0"  我想把这个date与今天的date进行比较(即我想知道这个$var是否在今天之前或者等于今天) 
我需要使用什么function?
 strtotime($var); 
将其转换为时间值
 time() - strtotime($var); 
 给你$var以来的秒数 
 if((time()-(60*60*24)) < strtotime($var)) 
 将检查$var是否在最后一天之内。 
该格式对于标准string比较来说是非常合适的,例如
 if (date1 > date2) 
 要获得这种格式的date,只需使用: date("Ymd H:i:s") 。 
所以:
 $today = date("Ymd H:i:s"); $date = "2010-01-21 00:00:00"; if ($date < $today) { 
这是这种格式的美:它很好地命令。 当然,这可能是效率较低,取决于具体的情况,但也可能更方便,导致更易于维护的代码 – 我们需要了解更多才能真正做出判断。
对于正确的时区,您可以使用,例如,
 date_default_timezone_set('America/New_York'); 
点击这里查看可用的PHP时区。
干得好:
 function isToday($time) // midnight second { return (strtotime($time) === strtotime('today')); } isToday('2010-01-22 00:00:00.0'); // true 
另外,还有一些辅助function:
 function isPast($time) { return (strtotime($time) < time()); } function isFuture($time) { return (strtotime($time) > time()); } 
要完成BoBby Jack,使用DateTime OBject,如果你有php 5.2.2+:
 if(new DateTime() > new DateTime($var)){ // $var is before today so use it } 
 你可以使用DateTime类: 
 $past = new DateTime("2010-01-01 00:00:00"); $now = new DateTime(); $future = new DateTime("2020-01-01 00:00:00"); 
比较操作员工作*:
 var_dump($past < $now); // bool(true) var_dump($future < $now); // bool(false) var_dump($now == $past); // bool(false) var_dump($now == new DateTime()); // bool(true) var_dump($now == $future); // bool(false) var_dump($past > $now); // bool(false) var_dump($future > $now); // bool(true) 
也可以从DateTime对象中获取时间戳值并进行比较:
 var_dump($past ->getTimestamp()); // int(1262286000) var_dump($now ->getTimestamp()); // int(1431686228) var_dump($future->getTimestamp()); // int(1577818800) var_dump($past ->getTimestamp() < $now->getTimestamp()); // bool(true) var_dump($future->getTimestamp() > $now->getTimestamp()); // bool(true) 
  *请注意,比较两个不同的DateTime对象时,即使它们表示相同的date, ===也会返回false。 
 $toBeComparedDate = '2014-08-12'; $today = (new DateTime())->format('Ym-d'); //use format whatever you are using $expiry = (new DateTime($toBeComparedDate))->format('Ym-d'); var_dump(strtotime($today) > strtotime($expiry)); //false or true 
几年后,我第二个鲍比杰克的观察,过去24小时不是今天! 我很惊讶,答案是如此之高…
要比较某个date是否小于,等于或大于另一个date,首先需要将它们“closures”到一天的开始。 换句话说,确保你在两个date的时间都是相同的00:00:00。 这可以简单而优雅地完成:
 strtotime("today") <=> strtotime($var) 
如果$ var具有00:00:00的时间部分,就像指定的OP一样。
用你需要的任何东西来replace<=>(或者像这样在php 7中保存)
另外,显然,我们正在讨论两者的同一个时区。
 一个基于我的经验的谨慎, 如果你的目的只涉及date,那么要小心,包括时间戳 。 例如,今天说的是"2016-11-09" 。 涉及时间戳的比较将使这里的逻辑无效。 例, 
 // input $var = "2016-11-09 00:00:00.0"; // check if date is today or in the future if ( time() <= strtotime($var) ) { // This seems right, but if it's ONLY date you are after // then the code might treat $var as past depending on // the time. } 
上面的代码似乎是正确的,但如果它只是你想比较的date,那么上面的代码是不正确的逻辑。 为什么? 因为,time()和strtotime()将提供包括时间戳。 也就是说,尽pipe这两个date都是在同一天,但时间上的差别将会很重要。 考虑下面的例子:
 // plain date string $input = "2016-11-09"; 
 由于input是纯datestring,因此在$input上使用strtotime()将假定它是2016-11-09的午夜。 所以,在午夜之后的任何时候运行time()都会把$input当作过去,尽pipe它们在同一天。 
要解决这个问题,你可以简单地编码,就像这样:
 if (date("Ymd") <= $input) { echo "Input date is equal to or greater than today."; } 
 $date1=date_create("2014-07-02"); $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2); 
(w3schools的例子,它完美的作品)
其他有用的例子可以是简单的PHP类。
它提供了许多方法以及比较function。
例如:
 $date = new simpleDate(); echo $date->now()->compare('2010-01-21 00:00:00')->isBefore(); 
教程页面中也有很多例子。 请点击这里 。