Solved

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

Posted on 2012-04-04
151 Views
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";
``````
0
Question by:tjyoung
• 2
• 2

LVL 2

Accepted Solution

joshfraser earned 500 total points
ID: 37805811
Try the following:

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

Josh
0

LVL 1

Author Comment

ID: 37805840
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?
0

LVL 1

Author Closing Comment

ID: 37805858
That was all I actually needed to complete afterall.
thanks.
0

LVL 2

Expert Comment

ID: 37805863
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);
``````
0

## Featured Post

### Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.