note 33680 deleted from function.date by sniper
| From: | sniper@php.net | Date: | Sat, 11 Oct 2003 16:54:30 +0000 |
| Subject: | note 33680 deleted from function.date by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58299@lists.php.net to get a copy of this message | ||
Note Submitter: tjw@tjw.org
----
// dateadd($datestr, $num, $unit)
// $datestr is a string that strtotime() is able to parse
// $num is the number of units to add
// $unit is either "Y", "m", "d", "H", "i",
"s" as understood by date()
// returns a UNIX timestamp of the resulting date
function dateadd($datestr, $num, $unit) {
$units =
array("Y","m","d","H","i","s");
$unix = strtotime($datestr);
while(list(,$u) = each($units)) $$u = date($u, $unix);
$$unit += $num;
return mktime($H, $i, $s, $m, $d, $Y);
}
// Examples
// Add 5 days
echo date("Y-m-d H:i:s", dateadd("2003-04-12", 5, "d"));
// Test Leap Year
echo date("Y-m-d H:i:s", dateadd("2004-02-28 23:31:00", 30, "i"));