SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  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,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Why would it be 2013-10? We are in December, one month ago is November....

  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,253
    Mentioned
    196 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,234
    Mentioned
    154 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.

  8. #8
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    That works today because you are on the 31st. But if you were on the first of December, that'd fail too.
    agree

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 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');

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 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');

  11. #11
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    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');
    Thanks cpradio for the valuable code.
    ill check it and feedback.

  12. #12
    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
    Thanks cpradio for the valuable code.
    ill check it and feedback.
    mean time following code also works today. but not sure any feature bugs will arise.

    Code:
    <?php
    $d = new DateTime(date( 'Y-m-d' ));
    $d->setDate( $d->format( 'Y' ), $d->format( 'm' ), 1 );
    $d->modify( '-1 month' );
    echo $d->format( 'Y-m' ), "\n";
    ?>
    2013-11

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by afridy View Post
    mean time following code also works today. but not sure any feature bugs will arise.

    Code:
    <?php
    $d = new DateTime(date( 'Y-m-d' ));
    $d->setDate( $d->format( 'Y' ), $d->format( 'm' ), 1 );
    $d->modify( '-1 month' );
    echo $d->format( 'Y-m' ), "\n";
    ?>
    2013-11
    Yes, that looks like it works in the edge cases too.

  14. #14
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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');
    This code too seems works well for me.

  15. #15
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Yes, that looks like it works in the edge cases too.
    tx for the confirmation

  16. #16
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by afridy View Post
    tx for the confirmation
    Not a problem, so now you have two pieces, one that will give you the last day of the prior month and the other (which you found) gives you the 1st day of the prior month. Both seem like they may suit you just fine

  17. #17
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Not a problem, so now you have two pieces, one that will give you the last day of the prior month and the other (which you found) gives you the 1st day of the prior month. Both seem like they may suit you just fine
    ofcause!

    HAPPY NEW YEAR .;'[]}{-+~!#$%^&*()


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
  •