daylight savings time messing up date calculation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

daylight savings time messing up date calculation

Post by rhecker »

I need to calculate the number of days between two dates as an integer. Daylight savings time screws up the number of days. My code is as follows:

$firstdate = strtotime($courseset["startdate"]);
$seconddate = strtotime($courseset["enddate"]);
$days = ($seconddate - $firstdate) / (60 * 60 * 24);

If I add the following to force $days to be an integer, it doesn't round correctly:

$days = (int) $days;
maneetpuri
Forum Commoner
Posts: 60
Joined: Tue Oct 07, 2008 6:32 am

Re: daylight savings time messing up date calculation

Post by maneetpuri »

Hi,

Try using functions round() and ceil(), this should solve the problem.

Cheers,
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: daylight savings time messing up date calculation

Post by rhecker »

Beautiful. Thank you. Thanks for mentioning ceil() as well as round().
Post Reply