Solved

mktime, back a month

Posted on 2003-11-30
2
228 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
Comment Utility
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
Comment Utility

Thank you loz, works great :)

Chris
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
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 …

763 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

11 Experts available now in Live!

Get 1:1 Help Now