Solved

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

Posted on 2012-04-04
4
155 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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