我试图以这种格式循环一些日期:" 2016年5月10日"并查看今天是在此之前还是之后,以显示/隐藏与该日期相关的div。
我到目前为止已经搜索过,并且只找到了比较只是数字日期的问题,但这种比较的正确代码是什么:
$cDate = "May 10, 2016"
$todayDate = NOW(); // in "May 25, 2016" format
if ($cDate < $todayDate) {
...more code...
}
答案 0 :(得分:3)
PHP的DateTime()
类解决方案很简单。
<?php
$date = new DateTime('May 30, 2016');
$today = new DateTime();
if($today > $date) {
echo "Date was in past";
} else if ($today == $date) {
echo "It's now";
} else {
echo "Date is in future";
}
答案 1 :(得分:2)
如果你不关心时区:
$that = strtotime("May 10, 2016");
$now = time();
if ($that < $now) {
// do your thing
}
答案 2 :(得分:1)
使用strtotime()
很容易$cDate = "May 10, 2016";
$todayDate = strtotime(date('Y-m-d')); // in "May 25, 2016" format
if (strtotime($cDate) < $todayDate) {
echo 'hi';
}