使用PHP,我希望将UNIX时间戳转换为类似于以下内容的日期字符串:2008-07-17 t9:24:17 z
如何将时间戳(如1333699439)转换为2008-07-17 t9:24:17 z ?
使用PHP,我希望将UNIX时间戳转换为类似于以下内容的日期字符串:2008-07-17 t9:24:17 z
如何将时间戳(如1333699439)转换为2008-07-17 t9:24:17 z ?
当前回答
假设你正在使用PHP5.3,那么现代的处理日期的方式是通过本机DateTime类。要获得当前时间,只需调用
$currentTime = new DateTime();
从一个特定的时间戳(即不是现在)创建一个DateTime对象
$currentTime = DateTime::createFromFormat( 'U', $timestamp );
要获得格式化的字符串,您可以调用
$formattedString = $currentTime->format( 'c' );
在这里查看手册页
其他回答
试试gmdate吧:
<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
<?php
$timestamp=1486830234542;
echo date('Y-m-d H:i:s', $timestamp/1000);
?>
假设你正在使用PHP5.3,那么现代的处理日期的方式是通过本机DateTime类。要获得当前时间,只需调用
$currentTime = new DateTime();
从一个特定的时间戳(即不是现在)创建一个DateTime对象
$currentTime = DateTime::createFromFormat( 'U', $timestamp );
要获得格式化的字符串,您可以调用
$formattedString = $currentTime->format( 'c' );
在这里查看手册页
设置默认时区以获得正确的结果是非常重要的
<?php
// set default timezone
date_default_timezone_set('Europe/Berlin');
// timestamp
$timestamp = 1307595105;
// output
echo date('d M Y H:i:s Z',$timestamp);
echo date('c',$timestamp);
?>
在线转换帮助:http://freeonlinetools24.com/timestamp
我发现这个对话中的信息非常有用,所以我只想补充一下我是如何使用MySQL数据库中的时间戳和一点PHP来计算它的
<?= date("Y-m-d\TH:i:s\+01:00",strtotime($column['loggedin'])) ?>
输出为:2017-03-03T08:22:36+01:00
非常感谢,Stewe,你的回答让我顿悟。