我有一个PHP事件的日历,它查询数据库以获取日期。这种格式PHP检查日期是否过去,以某种格式给出的某个日期
$event['date']
,这显示的:
我使用显示事件日期
2013-07-31
例如。
现在,我需要做的是检查这个日期是否是过去的日期到当前日期。
我该怎么做?
我有一个PHP事件的日历,它查询数据库以获取日期。这种格式PHP检查日期是否过去,以某种格式给出的某个日期
$event['date']
,这显示的:
我使用显示事件日期
2013-07-31
例如。
现在,我需要做的是检查这个日期是否是过去的日期到当前日期。
我该怎么做?
你可以用PHP的DateTime
类比较的日期:
$date = new DateTime($event['date']);
$now = new DateTime();
if($date < $now) {
echo 'date is in the past';
}
注意:使用DateTime
类优于strtotime()
因为后者会的日期前2038只读工作更多关于Year_2038_problem。
您可以使用strtotime()
和time()
:
if (strtotime($event['date']) < time()) {
// past date
}
if (time() > strtotime($event['date']))
{
// current date is greater than 2013-07-31
}
strtotime解析使用these rules日期刺痛。
@ Satch3000你已经接受了错误的答案为正确的解决方案(@Amal穆拉利)
请参阅输出,在这里我输入今天几号,但它返回当前日期为过去的日期。
<?php
/* Enter today date */
$date = new DateTime("09/14/2017");
$now = new DateTime();
print_r($date);
print_r($now);
if($date < $now) {
echo 'date is in the past';
}
输出将被
DateTime Object
(
[date] => 2017-09-14 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
DateTime Object
(
[date] => 2017-09-14 07:12:52.000000
[timezone_type] => 3
[timezone] => UTC
)
date is in the past
解
$Date1 = strtotime(date('Y-m-d', strtotime('2017-09-15'))).' ';
$Date2 = strtotime(date('Y-m-d'));
if($Date1 < $Date2) {
echo 'date is in the past';
}