如何在php中比较两个日期 [英] How to compare two dates in php
本文介绍了如何在php中比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果日期格式为'03_01_12'
和'31_12_11'
,如何在php中比较两个日期.
How to compare two dates in php if dates are in format '03_01_12'
and '31_12_11'
.
我正在使用以下代码:
$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
但是它不起作用.
推荐答案
您将必须确保您的日期是有效的日期对象.
You will have to make sure that your dates are valid date objects.
尝试一下:
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
然后您可以执行strtotime()
方法来获得差异.
You can then perform the strtotime()
method to get the difference.
这篇关于如何在php中比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文