SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Hybrid View

  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    php populer date bug, could not find a solution. pls help

    Hai folks

    PHP Code:
    $chk_dt=date('Y-m'strtotime('-1 month'));
    $chk_dt=date("Y-m"strtotime("this month -1 month")); 
    If you run now, it would return : 2013-11.
    but it must be 2013-10.

    So whats the fix for this? i could not find a solution in google. please help me.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Why would it be 2013-10? We are in December, one month ago is November....
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by afridy View Post
    Hai folks

    PHP Code:
    $chk_dt=date('Y-m'strtotime('-1 month'));
    $chk_dt=date("Y-m"strtotime("this month -1 month")); 
    If you run now, it would return : 2013-11.
    but it must be 2013-10.

    So whats the fix for this? i could not find a solution in google. please help me.
    sorry, again messed up.

    actually :

    PHP Code:
    $chk_dt=date('Y-m'strtotime('-1 month'));
    $chk_dt=date("Y-m"strtotime("this month -1 month")); 
    this returns 2013-12 instead of 2013-11

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Ah, I was wondering.
    I noticed some problems when trying to use "month" years ago, and had some hope it had been worked out by now, apparently not

    It seems that what I consider to be a month varies, i.e. 30 days, 31 days, 28/29 for February, from what PHP considers to be a "month", i.e. "30 days" regardless.

    That is, "month" seems to work OK most times, except for "beginnings/endings" if you know what I mean.

    Perhaps it would be better to use something other than "month"?

  5. #5
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Ah, I was wondering.
    I noticed some problems when trying to use "month" years ago, and had some hope it had been worked out by now, apparantly not

    It seems that what I consider to be a month varies, i.e. 30 days, 31 days, 28/29 for February, from what PHP considers to be a "month", i.e. "30 days" regardless.

    That is, "month" seems to work OK most times, except for "beginnings/endings" if you know what I mean.

    Perhaps it would be better to use something other than "month"?
    Thanks for the explanation to the bug. i understood now. require a concrete solution as my project is all about time sheets

  6. #6
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    what about this solution ? it shows correctly.

    Code:
    echo date('Y-m', (strtotime('-1 month')-86400)); // seconds in a day = 86400

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by afridy View Post
    what about this solution ? it shows correctly.

    Code:
    echo date('Y-m', (strtotime('-1 month')-86400)); // seconds in a day = 86400
    That works today because you are on the 31st. But if you were on the first of December, that'd fail too.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Here is my quick test on a way to resolve this, and it seems to work, but there are a variety of solutions.

    If you always need to get to the last day of the prior month, the below code works:
    PHP Code:
    $today = new DateTime('2013-12-31'); // remove the date string if you want to use today's date
    $todaysDate $today->format('d'); // Get today's date, so you know how many days to subtract
    $lastDayOfPriorMonth $today->sub(new DateInterval("P{$todaysDate}D"))->format('Y-m-d'); // Outputs last day of prior month 
    Here is my test for the entire calendar year of 2013
    PHP Code:
    <?php
    $dayOfTest 
    = new DateTime('2013-12-31');
    do
    {
        
    $today = new DateTime($dayOfTest->format('Y-m-d'));
        
    $todaysDate $today->format('d');
        echo 
    'Day of Test: ' $dayOfTest->format('Y-m-d') .
            
    'One Month Ago: ' $today->sub(new DateInterval("P{$todaysDate}D"))->format('Y-m-d') .
            
    '<br />';
        
    $dayOfTest->sub(new DateInterval('P1D'));
    } while (
    $dayOfTest->format('Y-m-d') != '2012-12-31');
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Also note, you can read the "number of days within the given month" to use in the subtraction, but that won't give you the results you want. As seen in the test below: 2013-12-01 then goes to 2013-10-31.
    PHP Code:
    <?php
    $dayOfTest 
    = new DateTime('2012-12-31');
    do
    {
        
    $today = new DateTime($dayOfTest->format('Y-m-d'));
        
    $todaysDate $today->format('t'); // line changed
        
    echo 'Day of Test: ' $dayOfTest->format('Y-m-d') .
            
    'One Month Ago: ' $today->sub(new DateInterval("P{$todaysDate}D"))->format('Y-m-d') .
            
    '<br />';
        
    $dayOfTest->sub(new DateInterval('P1D'));
    } while (
    $dayOfTest->format('Y-m-d') != '2011-12-31');
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


Bookmarks

Posting Permissions

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