?
Solved

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

Posted on 2012-04-04
4
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
joshfraser earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 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