note 101156 modified in function.date by danbrown
| From: | danbrown@php.net | Date: | Tue, 30 Nov 2010 15:02:31 +0000 |
| Subject: | note 101156 modified in function.date by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174473@lists.php.net to get a copy of this message | ||
Simple and handy function that offsets a MySQL formatted date and returns the new correctly
formatted date
<?php
/**
*
* @param string $dt // MySQL formatted date (like 2010-01-01)
* @param int $year_offset // like 2 or -2, or 5 or -5 ...
* @param int $month_offset // like 2 or -2, or 5 or -5 ...
* @param in $day_offset // like 2 or -2, or 5 or -5 ...
* @return string // the new MySQL formatted date (like 2009-07-01)
*/
function
MySQLDateOffset($dt,$year_offset='',$month_offset='',$day_offset='')
{
return ($dt=='0000-00-00') ? '' :
date ("Y-m-d", mktime(0,0,0,substr($dt,5,2)+$month_offset,substr($dt,8,2)+
$day_offset,substr($dt,0,4)+$year_offset));
}
?>
Examples:
<?php
echo MySQLDateOffset('2010-06-01',2,5,15);
// will return 2012-11-16
echo MySQLDateOffset('2010-06-01',-6,5,38);
// will return 2004-12-09
?>
--was--
/**
* Simple and handy function that offsets a MySQL formatted date
* and returns the new correctly formatted date
*
* @param string $dt // MySQL formatted date (like 2010-01-01)
* @param int $year_offset // like 2 or -2, or 5 or -5 ...
* @param int $month_offset // like 2 or -2, or 5 or -5 ...
* @param in $day_offset // like 2 or -2, or 5 or -5 ...
* @return string // the new MySQL formatted date (like 2009-07-01)
*/
function
MySQLDateOffset($dt,$year_offset='',$month_offset='',$day_offset='')
{
return ($dt=='0000-00-00') ? '' :
date ("Y-m-d", mktime(0,0,0,substr($dt,5,2)+$month_offset,substr($dt,8,2)+
$day_offset,substr($dt,0,4)+$year_offset));
}
Examples:
echo MySQLDateOffset('2010-06-01',2,5,15);
// will return 2012-11-16
echo MySQLDateOffset('2010-06-01',-6,5,38);
// will return 2004-12-09
http://php.net/manual/en/function.date.php