How to calculate days till some point in future in PHP?
I've a date like this: 2011-01-28 00:37:15
.
What would be the most efficien开发者_如何转开发t way to get days from now till this date? I want like number of full days till this date so I could display message: "after X days" rather than showing ugly date.
I am using php 5.2.6.
<?
$date = "2011-01-28 00:37:15";
$date_2 = date("Y-m-d H:i:s");
$date_diff=(strtotime($date)-strtotime($date_2)) / 86400;
?>
Have a look at http://de.php.net/manual/de/datetime.diff.php (PHP >=5.3.0)
This will return you a DateIntervall wich has a public attribute days
<?php
$datetime1 = new DateTime('2011-01-28 00:37:15');
$datetime2 = new DateTime('now');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days');
?>
Looks like this should help:
http://www.prettyscripts.com/code/php/php-date-difference-in-days
精彩评论