开发者

php date format converting problem

开发者 https://www.devze.com 2023-03-31 10:39 出处:网络
Here my function that i try to trasform dates into different formats. /*example: *dateString=\'03/25/2010\';

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消