[RESOLVED] Date increment by month: seemingly odd results from strtotime
Results 1 to 7 of 7

Thread: [RESOLVED] Date increment by month: seemingly odd results from strtotime

  1. #1
    Senior Member
    Join Date
    Nov 2009
    Posts
    150

    resolved [RESOLVED] Date increment by month: seemingly odd results from strtotime

    Hi everyone

    When looping and incrementing dates, I noticed that one date was incrementing incorrectly. On checking the code I realised I'd done it a rather poor way and fixed it, but does
    anyone know why I get the results shown?

    PHP Code:
    $monthnumber 1;
    $from date('Y-m-01');
    $to date('Y-m-t');

    while (
    $monthnumber 4) {
            
        print (
    "Monthnumber $monthnumber From $from to $to<br />");
            
    $from strtotime($from);
    $from date("Y-m-01"strtotime("+1 month"$from));

    // returns an incorrect $to date for February
    // $to = strtotime($to);
    // $to = date("Y-m-t", strtotime("+1 month", $to)); 

    // this works properly (and is obviously neater)
    $to date("Y-m-t"strtotime($from));

    $monthnumber++;
    // done all months 
    Correct output from code above:

    Monthnumber 1 From 2012-12-01 to 2012-12-31
    Monthnumber 2 From 2013-01-01 to 2013-01-31
    Monthnumber 3 From 2013-02-01 to 2013-02-28

    Incorrect output from code if I revert to the commented out $to code:

    Monthnumber 1 From 2012-12-01 to 2012-12-31
    Monthnumber 2 From 2013-01-01 to 2013-01-31
    Monthnumber 3 From 2013-02-01 to 2013-03-31

    Thank you

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,478
    Maybe you could use the "first day of next month" and/or "last day of next month" strotime() expressions, if that's what you ultimately want?
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Senior Member
    Join Date
    Mar 2009
    Posts
    1,144
    It's probably because you're using 't' in your date() function instead of something like 'd' or 'j'. I can't say for sure that's the issue but they are different and it's what stuck out to me immediately.

    The best way of incrementing dates (or working with dates/times in general) in my opinion is using the DateTime class.
    Declare variables, not war.

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,313
    $to starts with December 31; December 31 + 1 month is January 31; January 31 + 1 month is February 31.

    February 31? That's three days after February 28. So it's either March 2 or 3 depending on whether or not it's a leap year.

    The problems would actually continue past this: March 31 + 1 month is April 31 (i.e. the day after April 30).


    (Even if that all looks weird, it is consistent, and mimics how addition gets done ("April 30 + 1; 1, and carry the month...").
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,220
    No comment on the OP's issue, you guys have offered plenty of pointers.

    Interesting story, somewhat related ... yesterday I was updating a calendar routine I wrote last month to show "the previous month", and it showed that November 4th occurred twice. Had me scratching my head for a while until I realized that 4 Nov was the day after the US went back to Standard Time from Daylight Savings. I was doing something like "$ts+=86400" to loop through days; when the calendar added that extra hour ... [insert lightbulb moment here] ... problem now solved.

    If there's a "moral" to this story, it's "use PHP built-in functions whenever possible"
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,435
    Quote Originally Posted by dalecosp View Post
    I was doing something like "$ts+=86400" to loop through days
    Reminds me of my reply here.

  7. #7
    Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Thanks everyone for the usual enlightening help!

    NogDog, I didn't know about the "first day of next month" and/or "last day of next month" strotime() expressions. I can't find a definitive list of strtotime options but I'm sure there's one somewhere. strtotime is amazing.

    Bonesnap, thanks for the DateTime class which I didn't know about either. I do a lot of date manipulation so that'll help a lot.

    Weedpacket, great point, I can see the consistency now that you point it out. Of course, strtotime doesn't know that adding one month to a date means I necessarily want the last day of the month; even if the current date 'happens' to be the last month day. I presume it just increments the month number and applies a date format to it; I guess that step increments the year and resets the month to 1 if required.

    dalecosp and bradgradfelman demonstrate the point further very well; use PHP built-in functions to work with dates.

    Thanks again everyone.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •