开发者

how to check which date comes first in a year on comparing two dates using php

开发者 https://www.devze.com 2023-02-21 22:03 出处:网络
I have to find which date comes first in a year comparing two dates. For Example i want to display the date which comes first in year. FirstDate=2/3/2011 and SecondDate=1/1/2011

I have to find which date comes first in a year comparing two dates. For Example i want to display the date which comes first in year. FirstDate=2/3/2011 and SecondDate=1/1/2011 I should ge开发者_如何学Ct the answer as 1/1/2011 how to compare the two dates


You can compare date with strtotime function

$date1=strtotime('2/3/2011');
$date2=strtotime('1/1/2011');

if ($date1 < $date2)
{
   echo '2/3/2011 come first';
}
else
{
   echo '1/1/2011 come first';
}

But be aware of 2038 bug

Unix timestamps cannot deal with dates before Fri, 13 Dec 1901 20:45:54 UTC and after Tue, 19 Jan 2038 03:14:07 UTC


In javascript you can use Date :

var d1 = new Date(2011, 2, 2);
var d2 = new Date(2011, 0, 1);
if (d1 > d2) {
    alert('Date 1 is greater than Date 2');
}
...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号