从Facebook API转换PHP日期

时间:2013-08-12 13:20:56

标签: php datetime

通过以适当格式转换日期我遇到了问题。 我从Facebook API获得这种格式的时间:2013-08-23T09:00:00 然后我$fbdate = date('2013-08-23T09:00:00');

当我echo $fbdate时,它会重新2013-08-25UTC05:00:00

然后我尝试了:

$datum = date("d.m.Y",$fbdate);
$uhrzeit = date("H:i",$fbdate);

提取日期和时间但总是返回: 01.01.1970$datum00:33$uhrzeit

5 个答案:

答案 0 :(得分:8)

您应该使用strtotime()将日期字符串解析为UNIX时间戳:

$fbdate = strtotime('2013-08-23T09:00:00');

$datum = date('d.m.Y', $fbdate);
$uhrzeit = date('H:i', $fbdate);

答案 1 :(得分:5)

尝试使用DateTime类:

$fbdate = '2013-08-23T09:00:00';
$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $fbdate);

$datum = $date->format('d.m.Y');
$uhrzeit = $date->format('H:i');

echo $datum;
echo $uhrzeit;

答案 2 :(得分:1)

$datum = date("d.m.Y",strtotime($fbdate));
$uhrzeit = date("H:i",strtotime($fbdate));

PHP中的date函数旨在将microtime转换为日期,因此您需要先将字符串日期转换为microtime

答案 3 :(得分:1)

你试过这个吗?使用strtotime()功能时,您应该使用date

$datum = date("d.m.Y", strtotime($fbdate));
$uhrzeit = date("H:i", strtotime($fbdate));

答案 4 :(得分:1)

使用 strtotime()

$a = date("Y-M-d", strtotime($datum)); 
echo $a.$uhrzeit;

更多信息http://php.net/manual/en/function.strtotime.php