Page 1 of 1

mktime error php v5.2

Posted: Wed Aug 24, 2011 4:56 pm
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

Re: mktime error php v5.2

Posted: Fri Aug 26, 2011 9:38 am
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