PHP检查日期是否已经过了以特定格式给出的特定日期 [英] PHP Check if date is past to a certain date given in a certain format
本文介绍了PHP检查日期是否已经过了以特定格式给出的特定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个php事件的日历,它查询数据库以获取日期.
I have a php event's calender which queries the database to get the dates.
我使用以下方式显示活动日期:
I display the event date using:
$event['date']
,此显示的格式如下:
2013-07-31
.
现在,我需要检查此日期是否为当前日期的过去日期.
Now, what I need to do is to check if this date is a past date to the current date.
我该怎么做?
推荐答案
您可以将日期与PHP的 DateTime
类:
You can compare the dates with PHP's DateTime
class:
$date = new DateTime($event['date']);
$now = new DateTime();
if($date < $now) {
echo 'date is in the past';
}
注意:与strtotime()
相比,首选使用DateTime
类,因为后者仅适用于2038年之前的日期.了解有关年份_2038_问题.
Note: Using DateTime
class is preferred over strtotime()
since the latter will only work for dates before 2038. Read more about the Year_2038_problem.
这篇关于PHP检查日期是否已经过了以特定格式给出的特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文