Page 1 of 1
Calculating last month date
Posted: Sun Dec 26, 2004 8:32 pm
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?
Posted: Sun Dec 26, 2004 8:37 pm
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;
?>
Re: Calculating last month date
Posted: Sun Dec 26, 2004 8:57 pm
by njwan
Code: Select all
$lastmonth = date("Y") . "/" .(date("m")-1). "/" . date("d");
Posted: Sun Dec 26, 2004 9:05 pm
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"));
Posted: Sun Dec 26, 2004 9:17 pm
by tdelobe
Thanks!! This one worked.....fiiiiiiiiiiiinally!
Code: Select all
$lastmonth = date("Y\/m\/d", strtotime("-1 month"));
Posted: Sun Dec 26, 2004 9:30 pm
by Robert Plank
Yay!