mktime error php v5.2

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
cl2606
Forum Newbie
Posts: 9
Joined: Thu Jul 07, 2011 12:40 pm

mktime error php v5.2

Post by cl2606 »

I came across an error using mktime and php v5.2.11 (using wamp) and v5.2.15 (the host I'm using). When I tried it out on v5.3.5 using wamp it work fine.
Here's the code which calculates the number of days between two dates.

Code: Select all

function numberDays($date1, $date2) {

  // make dates using mktime because PHP v < 5.3
  $dateExplode1 = explode("-", $date1->format('m-d-Y'));
  $dateExplode2 = explode("-", $date2->format('m-d-Y'));
  
//  echo $date1->format('m-d-Y') . "<br />";
//  echo $date2->format('m-d-Y') . "<br />";
  
  $ts1 = mktime(0,0,0, $dateExplode1[0], $dateExplode1[1], $dateExplode1[2]);
  $ts2 = mktime(0,0,0, $dateExplode2[0], $dateExplode2[1], $dateExplode2[2]);
  
  $numSeconds = abs($ts1 - $ts2);
  
//  echo "numSeconds = $numSeconds<br />";
  
  
  $numDays = floor($numSeconds / 86400); // 86400 = 60 * 60 * 24
//  echo "numDays = $numDays<br /><br />";
  
  
  return $numDays;  
}
When I calculate the number of days between 2011-1-1 and 2011-3-14 I get the following:
numDays = 71 (using PHP v5.2.x)
numDays = 72 (using PHP v5.3.5) and this is the correct one.

Any fixes for this other than the obvious of getting a new host.

Thanks
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: mktime error php v5.2

Post by Jade »

Perhaps its a difference in the time zone on the servers or the daylight savings time: http://php.net/manual/en/function.mktime.php
Post Reply