Solved

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

Posted on 2012-04-04
4
151 Views
Last Modified: 2012-04-04
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

0
Comment
Question by:tjyoung
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
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);

Open in new window


Josh
0
 
LVL 1

Author Comment

by:tjyoung
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

by:tjyoung
ID: 37805858
That was all I actually needed to complete afterall.
thanks.
0
 
LVL 2

Expert Comment

by:joshfraser
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);

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now