Hướng dẫn php datetime previous month

I want to get last month's date. I wrote this out:

$prevmonth = date['M Y'];

Which gives me the current month/year. I can't tell if I should use strtotime, mktime. Something to the timestamp? Do I need to add something afterwards to reset so that the date isn't set to last month throughout everything for all timestamps on my site? I'm trying to RTM but it's hard for me to figure this out.

asked Dec 11, 2009 at 17:52

It's simple to get last month date

echo date["Y-n-j", strtotime["first day of previous month"]];
echo date["Y-n-j", strtotime["last day of previous month"]];

at November 3 returns

2014-10-1
2014-10-31

Josafat

3323 silver badges15 bronze badges

answered Mar 3, 2011 at 12:32

OzzyCzechOzzyCzech

8,9272 gold badges46 silver badges31 bronze badges

3

echo strtotime["-1 month"];

That will output the timestamp for last month exactly. You don't need to reset anything afterwards. If you want it in an English format after that, you can use date[] to format the timestamp, ie:

echo date["Y-m-d H:i:s",strtotime["-1 month"]];

answered Dec 11, 2009 at 17:53

zombatzombat

90.9k24 gold badges155 silver badges164 bronze badges

4

$prevmonth = date['M Y', strtotime["last month"]];

answered Dec 11, 2009 at 17:55

ScharrelsScharrels

3,02824 silver badges31 bronze badges

1

Incorrect answers are:

$lastMonth = date['M Y', strtotime["-1 month"]];
$lastDate = date['Y-m', strtotime['last month']];

The reason is if current month is 30+ days but previous month is 29 and less $lastMonth will be the same as current month.

e.g.

If $currentMonth = '30/03/2016';
echo $lastMonth = date['m-Y', strtotime["-1 month"]]; => 03-2016
echo $lastDate = date['Y-m', strtotime['last month']]; => 2016-03

The correct answer will be:

echo date["m-Y", strtotime["first day of previous month"]]; => 02-2016
echo sprintf["%02d",date["m"]-1] . date["-Y"]; => 02-2016
echo date["m-Y",mktime[0,0,0,date["m"]-1,1,date["Y"]]]; => 02-2016

answered Mar 30, 2016 at 13:42

FuryFury

4,4734 gold badges46 silver badges78 bronze badges

1

Found this one wrong when the previous months is shorter than current.

echo date["Y-m-d H:i:s",strtotime["-1 month"]];

Try on March the 30th and you will get 2012-03-01 instead of 2012-02...

Working out on better solution...

answered Mar 30, 2012 at 7:38

Jay_69Jay_69

871 silver badge2 bronze badges

3

if you want to get just previous month, then you can use as like following

$prevmonth = date['M Y', strtotime['-1 months']];

if you want to get same days of previous month, Then you can use as like following ..

$prevmonth = date['M Y d', strtotime['-1 months']];

if you want to get last date of previous month , Then you can use as like following ...

$prevmonth = date['M Y t', strtotime['-1 months']];

if you want to get first date of previous month , Then you can use as like following ...

$prevmonth = date['M Y 1', strtotime['-1 months']];

answered Jan 13, 2013 at 7:38

2

echo date['Y',strtotime["-1 year"]];        //last year
echo date['d',strtotime["-1 day"]]; //last day
echo date['m',strtotime["-1 month"]]; //last month

answered Mar 30, 2012 at 9:29

VaishuVaishu

2,2553 gold badges22 silver badges25 bronze badges

1

public function getLastMonth[] {
    $now = new DateTime[];
    $lastMonth = $now->sub[new DateInterval['P1M']];
    return $lastMonth->format['Ym'];
}

answered Apr 19, 2012 at 7:33

1

Use this short code to get previous month for any given date:

$tgl = '25 january 2012';

$prevmonth = date["M Y",mktime[0,0,0,date["m", strtotime[$tgl]]-1,1,date["Y", strtotime[$tgl]]]];
echo $prevmonth;

The result is December 2011. Works on a month with shorter day with previous month.

Abdul Rahman

2,0874 gold badges28 silver badges36 bronze badges

answered May 31, 2012 at 10:00

abdulabdul

211 bronze badge

$lastMonth = date['M Y', strtotime["-1 month"]];
var_dump[$lastMonth];
$lastMonth = date['M Y', mktime[0, 0, 0, date['m'] - 1, 1, date['Y']]];
var_dump[$lastMonth];

answered Aug 26, 2015 at 5:46

1

You can use strtotime, which is great in this kind of situations :

$timestamp = strtotime['-1 month'];
var_dump[date['Y-m', $timestamp]];

Will get you :

string '2009-11' [length=7]

answered Dec 11, 2009 at 17:55

Pascal MARTINPascal MARTIN

388k77 gold badges646 silver badges656 bronze badges

$time = mktime[0, 0, 0, date["m"],date["d"]-date["t"], date["Y"]];
$lastMonth = date["d-m-Y", $time];

OR

$lastMonth = date["m-Y", mktime[] - 31*3600*24];

works on 30.03.2012

Juan Mellado

14.9k5 gold badges45 silver badges53 bronze badges

answered Mar 30, 2012 at 9:34

Oh I figured this out, please ignore unless you have the same problem i did in which case:

$prevmonth = date["M Y",mktime[0,0,0,date["m"]-1,1,date["Y"]]];

Taryn

237k55 gold badges362 silver badges402 bronze badges

answered Dec 11, 2009 at 17:55

pg.pg.

2,3874 gold badges40 silver badges64 bronze badges

This question is quite old but here goes anyway. If you're trying to get just the previous month and the day does not matter you can use this:

$date = '2014-01-03';

$dateTime = new DateTime[$date];

$lastMonth = $dateTime->modify['-' . $dateTime->format['d'] . ' days']->format['F Y'];

echo $lastMonth; // 'December 2013'

answered Nov 3, 2014 at 18:32

Jay KravetzJay Kravetz

2332 silver badges10 bronze badges

The best solution I have found is this:

function subtracMonth[$currentMonth, $monthsToSubtract]{
        $finalMonth = $currentMonth;
        for[$i=0;$i

Chủ Đề