Solved

mktime, back a month

Posted on 2003-11-30
2
230 Views
Last Modified: 2006-11-17

Hi,

I need to go back a month, then another, then another, etc.  I thought it went something like:

$date1 = date("M_y",mktime(0,0));
$date2 = date("M_y",mktime(-1,0));
$date3 = date("M_y",mktime(-2,0));
$date4 = date("M_y",mktime(-3,0));

but I obviously don't know what I am doing, I get: Dec_03, Nov_03, Nov_03, Nov_03 as the results.  What I need to get is: Dec_03, Nov_03, Oct_03, Sep_03.

What do I need to do to fix?

Thanks,  Chris


0
Comment
Question by:ChrisAndrews
2 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 50 total points
ID: 9848420
hi,

probably the easiest but not quite so technical way:

<?
$date1 = date("M_y"); // the current date in the format Dec_03
$date2 = strtotime("-1 month"); // timestamp of current date - 1 month
$date3 = strtotime("-2 month"); // timestamp of current date - 2 months
$date4 = strtotime("-3 month"); // timestamp of current date - 3 months
$date2 = date("M_y", $date2); // formatted string of 1 month back
$date3 = date("M_y", $date3); // formatted string of 2 months back
$date4 = date("M_y", $date4); // formatted string of 3 months back
?>

cheers,

loz
0
 

Author Comment

by:ChrisAndrews
ID: 9848481

Thank you loz, works great :)

Chris
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 look for a specific file type in a local or remote server directory using PHP.

786 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