How many days until X-Y-Z date?

Expanding on schnaader's answer, here is a one-liner function that takes a date string as a parameter but only returns the number of days:

<?php
function days_until($date){
    return (isset($date)) ? floor((strtotime($date) - time())/60/60/24) : FALSE;
}
?>

Don't treat dates as integers. Use your database, which has good support for dealing with calendars/time.

select datediff("2009-11-12", now())

       <?php
         $cdate = mktime(0, 0, 0, 12, 31, 2009);
         $today = time();
         $difference = $cdate - $today;
         if ($difference < 0) { $difference = 0; }
         echo floor($difference/60/60/24)." days remaining";
       ?>

PHP 5.3 has introduced the DateTime class that implements a 'diff' function. See http://www.php.net/manual/en/datetime.diff.php

Tags:

Datetime

Php