Php get how many days and hours left from a date

John123 picture John123 · Sep 19, 2011 · Viewed 44.9k times · Source

I have a created_at date saved liked this "2011-09-23 19:10:18" And I want to get the days and hours left until the date is reached. How do I do that? and column name in database remain days automatically update daily with remain days, please solve this

Answer

Rudu picture Rudu · Sep 19, 2011

PHP fragment:

<?php

//Convert to date
$datestr="2011-09-23 19:10:18";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)

//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));

//Report
echo "$days days $hours hours remain<br />";
?>

Note the hour-round and no minutes/seconds consideration means it can be slightly inaccurate.