PHP時間日期比較
阿新 • • 發佈:2019-01-26
相關文章:
若要使用php來比較日期,最好用DateTime::diff
但是這個是5.3才支援的,如果沒有這樣的環境,可以使用<、>來比較
如下例子,會輸出right
$date1=strtotime('2009-8-9 20:00');
$date2=strtotime('2009-9-9 1:00');
if($date1<$date2)
echo 'right';
else
echo 'wrong';
注:DateTime::diff
DateTime::diff
(PHP 5 >= 5.3.0)
DateTime::diff — Returns the difference between two DateTime objects
Description
public DateInterval DateTime::diff ( DateTime $datetime [, bool $absolute ] )
Returns the difference between two DateTime objects.
Parameters
- datetime
-
The date to compare to.
- absolute
-
Whether to return absolute difference. Defaults to FALSE.
Return Values
The difference between two dates.