Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate days till some point in future in PHP?

Tags:

date

php

I've a date like this: 2011-01-28 00:37:15. What would be the most efficient 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.

like image 861
Rihards Avatar asked Dec 29 '10 13:12

Rihards


2 Answers

<?

$date = "2011-01-28 00:37:15";
$date_2 = date("Y-m-d H:i:s");
$date_diff=(strtotime($date)-strtotime($date_2)) / 86400;

?>
like image 168
Lynn Rey Avatar answered Sep 20 '22 16:09

Lynn Rey


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

like image 24
Martin Holzhauer Avatar answered Sep 20 '22 16:09

Martin Holzhauer