Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting last month's date in php

Tags:

date

php

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.

like image 665
pg. Avatar asked Dec 11 '09 17:12

pg.


People also ask

How can I get the last date of last month in php?

Get Last Day Of The Previous Month:$lastDay = date('t',strtotime('last month')); print_r($lastDay);

How can I get first date and date of last month in php?

php echo 'First Date = ' . date('Y-m-01') . '<br />'; echo 'Last Date = ' . date('Y-m-t') .

What is Strtotime php?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


1 Answers

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 
like image 116
OzzyCzech Avatar answered Oct 05 '22 09:10

OzzyCzech