PDA

View Full Version : Finding the diffrence between two dates.

Ramesiv
12-31-2005, 05:37 PM
I'm currently using this little block of code here to find the diffrence (in days) between the current date, and the date of the new year

\$date_new_year = strtotime("1 January 2006");
\$current_date = time();
\$days_left = \$date_new_year - \$days_left;
print "Days left until new year's: ";
print date("d", \$days_left);

I'm not sure if this is valid, is there a function that allows me to get the exact days, months, years, hours, minutes and even seconds from a specifc date to the current date? Thanks for reading.

devinemke
12-31-2005, 06:58 PM
\$start_date = '2005-12-31';
\$end_date = '2006-06-03';

\$start_ts = strtotime(\$start_date);
\$end_ts = strtotime(\$end_date);
\$count = 0;
while (\$start_ts < \$end_ts)
{
\$start_ts = strtotime(date('Y-m-d', \$start_ts) . ' +1 day');
\$count++;
}
echo \$count;

Velox Letum
12-31-2005, 08:44 PM
<?php
\$date_new_year = strtotime("1 January 2006");
\$current_date = time();
\$days_left = \$date_new_year - \$current_date;
echo 'Days left until new years: ' . \$days_left / 60 / 60 / 24;
?>

If you want hours and such instead of a decimal you'll have to calculate it out further.

devinemke
12-31-2005, 08:55 PM
...echo 'Days left until new years: ' . \$days_left / 60 / 60 / 24;
...and what about the days of the year that are not 60x60x24 seconds long?