#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    393
    Rep Power
    47

    Error in date() function


    So... here's a quandry...

    I have written a simple calendar function which uses a start date and then increments by one day and populates a calendar for a given number of days. On my development server (MacBook Pro / XAMPP), the calendar is perfect. However, on the production server (not sure of the actual architecture), November 3rd appears twice.

    The abbreviated code looks like this:

    PHP Code:
    $calendar_startmktime(0,0,0,9,2,2013);
    $numberofweeks 52;
    $NumberOfDays $numberofweeks*7;

    for (
    $i=1$i <= $NumberOfDays$i++) {
      
    // Stuff to calculate day-of-week and table rows omitted

        
    $date_unix = ($calendar_start+($i-1)*60*60*24);
        
    $date date("M j", ($date_unix));

    If I output the values for $i and $date, I get the following:
    60 - Oct 31
    61 - Nov 1
    62 - Nov 2
    63 - Nov 3
    64 - Nov 3
    65 - Nov 4
    66 - Nov 5

    So, $i is incrementing correctly... is there a rounding error in my "$date_unix" calculation that I am missing? Why would it produce different results on two different servers?

    If my initial "mktime" used a different value for time-of-day, mktime(8,0,0,9,2,2013), would that be more reliable?

    Thanks for your input!
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Daylight savings. Do this:
    PHP Code:
    $calendar_startmktime(0,0,0,9,2,2013); 
    $numberofweeks 52
    $NumberOfDays $numberofweeks*7

    for (
    $i=1$i <= $NumberOfDays$i++) { 
      
    // Stuff to calculate day-of-week and table rows omitted 

        
    $date_unix = ($calendar_start+($i-1)*60*60*24); 
        
    $date date("M j", ($date_unix)); 
        
    $fulldate date('c'$date_unix);
        echo 
    "{$date} ({$fulldate})\n";

    You'll get:


    Code:
    Oct 30 (2013-10-30T00:00:00-04:00)
    Oct 31 (2013-10-31T00:00:00-04:00)
    Nov 1 (2013-11-01T00:00:00-04:00)
    Nov 2 (2013-11-02T00:00:00-04:00)
    Nov 3 (2013-11-03T00:00:00-04:00)
    Nov 3 (2013-11-03T23:00:00-05:00)
    Nov 4 (2013-11-04T23:00:00-05:00)
    Nov 5 (2013-11-05T23:00:00-05:00)
    Use the datetime class, or use mktime.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    393
    Rep Power
    47
    Well, I at least partially answered my own question. By using 8:00 AM in the start time/date, the calendar now works, although I am not sure how reliable it would be going out into the future.

    Possibly, there is an error introduced with daylight savings, but I am not sure.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Possibly, yes.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    393
    Rep Power
    47
    Thanks, ManiacDan.

    I posted my own reply before I noticed that you had, in fact replied with your daylightsavings post.

    Thanks for both replies!!

    Comments on this post

    • ManiacDan agrees : I know, I was just being sarcastic

IMN logo majestic logo threadwatch logo seochat tools logo