Calculating last month date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Calculating last month date

Post by tdelobe »

I can not figure out how to return a date in the form of 2004/12/26 but have it return today's date minus 1 month. I have this, but I am on windows and it can not take a negative number.

Code: Select all

$lastmonth = date("Y") . "/" . date("m")-1 . "/" . date("d");
So, on unix that would work, but not on windows. How can I get this to work?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Try:

Code: Select all

<?php
$year = date("Y");
$month = date("m");
$day = date("d");
$month_math = $month - 1;

$lastmonth = $year . "/" . $month_math . "/" . $day;
?>
njwan
Forum Newbie
Posts: 8
Joined: Wed Jul 28, 2004 1:52 am

Re: Calculating last month date

Post by njwan »

Code: Select all

$lastmonth = date("Y") . "/" .(date("m")-1). "/" . date("d");
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Those ways will still show 0 for the month if it is January. Do it this way:

Code: Select all

$lastmonth = date("Y\/m\/d", strtotime("-1 month"));
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Post by tdelobe »

Thanks!! This one worked.....fiiiiiiiiiiiinally!

Code: Select all

$lastmonth = date("Y\/m\/d", strtotime("-1 month"));
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Yay!
Post Reply