Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
163 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

618 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