Github API,解码“日期”

Im using Github's api to get my latest commits, and the date format returned looks like this

2012-01-25T11:23:28-08:00

I tried to do it like this:

$date = explode('T', $next['commit']['author']['date']);
$time = strtotime($date[0] .' '. $date[1]);
$date = date('M j, Y  g:i a', $time);

But it didnt turn out right as php thought I was subtracting 8 hours from the time (because of the timezone). I would like to keep the timezone but i have no clue how to parse that. Does anyone know how to have it where the time is correct and shows the time zone abbreviation (GMT, PST etc etc )?

It cannot get any simpler than this:

$a = new DateTime("2012-01-25T11:23:28-08:00");
echo $a->format("Y-m-d H:i:s");
//outputs 2012-01-25 11:23:28

See the documentation of the DateTime class for more info.

The simple, mechanical, solution is to break the date down yourself completely:

$date = substr($next['commit']['author']['date'], 0, 10);
$time = substr($next['commit']['author']['date'], 11, 9);
$zone = substr($next['commit']['author']['date'], 20, 6);

list($y, $m, $d) = explode('-', $date);
list($h, $i, $s) = explode(':', $time);

$zh = substr($zone, 1, 2);
$zm = substr($zone, 4, 2);

if (substr($zone, 0, 1) == '-'){
  $h -= $zh;
  $m -= $zm;
}else{
  $h += $zh;
  $m += $zm;
}

$ts = gmmktime($h,$i,$s,$m,$d,$y);

This will give you a timestamp in UTC.

The issue is with "shows the time zone abbreviation" - you can't find a abbreviation for a given offset, because there can be several - you can't tell which of the e.g. +01:00 timezones the date is in - could be european, african, or bristish summer time.

It really depends what you want to do with the data.