How to get previous month and year relative to today, using strtotime and date?

前端 未结 15 913
情书的邮戳
情书的邮戳 2020-11-28 07:48

I need to get previous month and year, relative to current date.

However, see following example.

// Today is 2011-03-30
echo date(\'Y-m-d\', strtotim         


        
相关标签:
15条回答
  • 2020-11-28 08:30
    date('Y-m', strtotime('first day of last month'));
    
    0 讨论(0)
  • 2020-11-28 08:31

    I found an answer as I had the same issue today which is a 31st. It's not a bug in php as some would suggest, but is the expected functionality (in some since). According to this post what strtotime actually does is set the month back by one and does not modify the number of days. So in the event of today, May 31st, it's looking for April-31st which is an invalid date. So it then takes April 30 an then adds 1 day past it and yields May 1st.

    In your example 2011-03-30, it would go back one month to February 30th, which is invalid since February only has 28 days. It then takes difference of those days (30-28 = 2) and then moves two days past February 28th which is March 2nd.

    As others have pointed out, the best way to get "last month" is to add in either "first day of" or "last day of" using either strtotime or the DateTime object:

    // Today being 2012-05-31
    //All the following return 2012-04-30
    echo date('Y-m-d', strtotime("last day of -1 month"));
    echo date('Y-m-d', strtotime("last day of last month"));
    echo date_create("last day of -1 month")->format('Y-m-d'); 
    
    // All the following return 2012-04-01
    echo date('Y-m-d', strtotime("first day of -1 month")); 
    echo date('Y-m-d', strtotime("first day of last month"));
    echo date_create("first day of -1 month")->format('Y-m-d');
    

    So using these it's possible to create a date range if your making a query etc.

    0 讨论(0)
  • 2020-11-28 08:31
    function getOnemonthBefore($date){
        $day = intval(date("t", strtotime("$date")));//get the last day of the month
        $month_date = date("y-m-d",strtotime("$date -$day days"));//get the day 1 month before
        return $month_date;
    }
    

    The resulting date is dependent to the number of days the input month is consist of. If input month is february (28 days), 28 days before february 5 is january 8. If input is may 17, 31 days before is april 16. Likewise, if input is may 31, resulting date will be april 30.

    NOTE: the input takes complete date ('y-m-d') and outputs ('y-m-d') you can modify this code to suit your needs.

    0 讨论(0)
  • 2020-11-28 08:32

    if i understand the question correctly you just want last month and the year it is in:

    <?php
    
      $month = date('m');
      $year = date('Y');
      $last_month = $month-1%12;
      echo ($last_month==0?($year-1):$year)."-".($last_month==0?'12':$last_month);
    
    ?>
    

    Here is the example: http://codepad.org/c99nVKG8

    0 讨论(0)
  • 2020-11-28 08:32

    ehh, its not a bug as one person mentioned. that is the expected behavior as the number of days in a month is often different. The easiest way to get the previous month using strtotime would probably be to use -1 month from the first of this month.

    $date_string = date('Y-m', strtotime('-1 month', strtotime(date('Y-m-01'))));
    
    0 讨论(0)
  • 2020-11-28 08:32

    Perhaps slightly more long winded than you want, but i've used more code than maybe nescessary in order for it to be more readable.

    That said, it comes out with the same result as you are getting - what is it you want/expect it to come out with?

    //Today is whenever I want it to be.
    $today = mktime(0,0,0,3,31,2011);
    
    $hour   = date("H",$today);
    $minute = date("i",$today);
    $second = date("s",$today);
    $month  = date("m",$today);
    $day    = date("d",$today);
    $year   = date("Y",$today);
    
    echo "Today: ".date('Y-m-d', $today)."<br/>";
    echo "Recalulated: ".date("Y-m-d",mktime($hour,$minute,$second,$month-1,$day,$year));
    

    If you just want the month and year, then just set the day to be '01' rather than taking 'todays' day:

     $day = 1;
    

    That should give you what you need. You can just set the hour, minute and second to zero as well as you aren't interested in using those.

     date("Y-m",mktime(0,0,0,$month-1,1,$year);
    

    Cuts it down quite a bit ;-)

    0 讨论(0)
提交回复
热议问题