Jump to content

Really strange problem involving Nov. 4, 2007


hoogie

Recommended Posts

This one doesn't make any sense.

 

I've been using this code to take $date (a date returned from a database) and add one day to it:

 

<?php
$nextdate = date("Y-m-d H:i:s", strtotime($date)+((60*60)*24));
?>

 

This works - I've put probably 6 months worth of data through it with no problem.

 

But when the date from the database is 2007-11-4, it only adds 23 hours.  I get 2007-11-4 23:00:00 back instead of 2007-11-5.

 

I've tested with many other dates including Nov. 4 from other years, and it works perfectly except for November 4, 2007.

 

I can get around this by adding 25 hours instead of 24 (I don't use the time, just the date, so it doesn't matter), but I'm just really wondering what's going on here?  Is this a bug in PHP?

 

I'm running:

XAMPP for Linux 1.6.5a

PHP 5.2.5

MySQL 5.0.51

Link to comment
Share on other sites

i suggest that you look into using mktime() to create times/dates. multiplying hours x number of days is asking for trouble, as you've found. maybe something like:

 

<?php
// Get this same time 6 months from now:
$current_time = strtotime($date);
$next_time = mktime(date("G",$current_time),intval(date("i",$current_time)),intval(date("s",$current_time)),date("n",$current_time) + 6, date("j",$current_time), date("Y",$current_time));
$nextdate = date("Y-m-d H:i:s", $next_time);
?>

Link to comment
Share on other sites

If you use the strtotime() date/time math parameters, it works correctly -

 

$nextdate = date("Y-m-d H:i:s", strtotime("$date + 1 day"));

 

2007-11-4 gives 2007-11-05 00:00:00

 

that's a good one. I'm not experienced with the nuances of strtotime() so i always fall back on mktime(). i also ran into this problem long ago while developing an employee time-tracking application. everything worked fine until one day in the spring... :-)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.