开发者

Calculate two dates with PHP?

I have two dates in the format below:

Start Date = 30-10-2009

End Date = 30-11-200开发者_StackOverflow9

How, with PHP could I calculate the seconds between these two dates?


Parse the two dates into Unix timestamps using strtotime, then get the difference:

$firstTime = strtotime("30-10-2009");
$secondTime = strtotime("30-11-2009");
$diff = $secondtime - $firstTime;


The function strtotime() will convert a date to a unix-style timestamp (in seconds). You should then be able to subtract the end date from the start date to get the difference.

$difference_secs = strtotime($end_date) - strtotime($start_date);


I'd rather advice to use built in DateTime object.

$firstTime = new DateTime("30-10-2009");
$diff = $firstTime->diff(new DateTime("30-11-2009"));

As for me it's more flexible and OOP oriented.


In fact previous answer will give you a DateInterval object but not seconds. In order to get seconds with OOP approach you should do this:

$date1 = new DateTime("30-10-2009");
$date2 = new DateTime("30-11-2009");
$seconds = $date2->getTimestamp() - $date1->getTimestamp();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜