php date format converting problem
Here my function that i try to trasform dates into different formats.
/* example:
* dateString = '03/25/2010';
* inputDateFormat = '%m/%d/%Y';
* ouputDateFormat = 'Y-m-d';
* return -> '2010-03-25';
*/
function formatDate($dateString,$inputFormat=NULL,$outputFormat=NULL){
if($dateString==''||$dateString==NULL) return '';
$t = strptime($dateString,$inputFormat);
return gmdate($outputFormat,mktime($t[tm_sec],$t[tm_min],$t[tm_hour],($t[tm_mon]+1),($t[tm_mday]+1),($t[tm_year]+1900)));
}
My problem is
when i try to convert this date Wed, 19 Jan 2011 21:16:37 +0000
into 2011-01-19 21:16:37
with the following line:
echo formatDate('Wed, 19 Jan 2011 21:16:37 +0000','%a, %d %b %Y %H:%M:%S','Y-m-d H:i:s');
the result开发者_开发技巧 is this:
2011-01-21 11:16:21
why i'm getting the date with 2 days extra. Do you have any idea?
use this instead:
function formatDate($dateString, $outputFormat=NULL){
return date($outputFormat, strtotime($dateString));
}
echo formatDate('Wed, 19 Jan 2011 21:16:37 +0000','Y-m-d H:i:s');
This is a wild guess, but maybe you need to set yoru time zone?
date_default_timezone_set() (requires PHP 5)
精彩评论