Yunus Eren Güzel
Yunus Eren Güzel

Reputation: 3088

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?

Upvotes: 2

Views: 494

Answers (2)

Book Of Zeus
Book Of Zeus

Reputation: 49895

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');

Upvotes: 5

sman591
sman591

Reputation: 560

This is a wild guess, but maybe you need to set yoru time zone?

date_default_timezone_set() (requires PHP 5)

Upvotes: 4

Related Questions