Link to home
Start Free TrialLog in
Avatar of tjyoung
tjyoung

asked on

Display 3 months ago time span taking into account not all months are 31 days... how?

I'm trying to determine how to display the following:
2012-01-01 (which is 3 months ago on the first of the month excluding the current month we're in)
My code below (which I'm certainly not married to if there is a better way), does the above
Problem is I also need to show the last day of the month, 3 months ago, taking into account not all months have 31 days??

How do you accomplish that?

$monthdate = date("Y-m");
$threemonth = strtotime ( '-3 month' , strtotime ( $monthdate ) ) ;
$threemonth = date ( 'Y-m' , $threemonth );
 echo $threemonth."-01";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of joshfraser
joshfraser
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tjyoung
tjyoung

ASKER

Hi,
thats great, shows me 3 months ago on the 1st of the month, cleaner then my method for sure.
How can I get it to show the last day of the month regardless of how many days are in the month?
Avatar of tjyoung

ASKER

That was all I actually needed to complete afterall.
thanks.
I'm not 100% if this will work correctly.

This should display 2012/1/31. Current month -2 and then back one day...

$threemonthsago = mktime(0,0,0,date("m")-2,-1,date("Y"));
echo date("Y/m/d", $threemonthsago);

Open in new window