This question already has an answer here:
这个问题已经有了答案:
- How to calculate the difference between two dates using PHP? 31 answers
- 如何使用PHP计算两个日期之间的差异?31日答案
I'm trying to cycle through some dates in this format: "May 10, 2016" and see if today is before or after that, for the purpose of showing/hiding div's associated with that date.
我正试着以这种格式循环一些日期:“2016年5月10日”,看看今天是在那之前还是之后,为了显示/隐藏div与那个日期相关。
I've searched so far, and only found questions where the comparison is just with numerical dates, but what would be the correct code for this sort of comparison:
到目前为止,我已经搜索过了,只找到了与数字日期进行比较的问题,但是这种比较的正确代码是什么呢?
$cDate = "May 10, 2016"$todayDate = NOW(); // in "May 25, 2016" formatif ($cDate < $todayDate) { ...more code...}
3 个解决方案
#1
2
Solution is simple with PHP's DateTime()
class.
解决方案使用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"; }
#2
2
If you don't care about timezones:
如果你不关心时区:
$that = strtotime("May 10, 2016");$now = time();if ($that < $now) { // do your thing}
#3
1
It's easy with strtotime()
很容易与strtotime()
$cDate = "May 10, 2016";$todayDate = strtotime(date('Y-m-d')); // in "May 25, 2016" formatif (strtotime($cDate) < $todayDate) { echo 'hi';}
#1
2
Solution is simple with PHP's DateTime()
class.
解决方案使用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"; }
#2
2
If you don't care about timezones:
如果你不关心时区:
$that = strtotime("May 10, 2016");$now = time();if ($that < $now) { // do your thing}
#3
1
It's easy with strtotime()
很容易与strtotime()
$cDate = "May 10, 2016";$todayDate = strtotime(date('Y-m-d')); // in "May 25, 2016" formatif (strtotime($cDate) < $todayDate) { echo 'hi';}