使用PHP,我希望将UNIX时间戳转换为类似于以下内容的日期字符串:2008-07-17 t9:24:17 z

如何将时间戳(如1333699439)转换为2008-07-17 t9:24:17 z ?


当前回答

使用date函数date (string $format [, int $timestamp = time()])

使用date('c',time())作为格式转换为ISO 8601日期(在PHP 5中添加)- 2012-04-06T12:45:47+05:30

使用date("Y-m-d\TH:i:s\Z",1333699439)得到2012-04-06T13:33:59Z

下面是date函数支持的一些格式

<?php
$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
?>

其他回答

试试gmdate吧:

<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>

你可以像.....

$originalDate = "1585876500";

echo $newDate = date("Y-m-d h:i:sa", date($originalDate));

假设你正在使用PHP5.3,那么现代的处理日期的方式是通过本机DateTime类。要获得当前时间,只需调用

$currentTime = new DateTime();

从一个特定的时间戳(即不是现在)创建一个DateTime对象

$currentTime = DateTime::createFromFormat( 'U', $timestamp );

要获得格式化的字符串,您可以调用

$formattedString = $currentTime->format( 'c' );

在这里查看手册页

<?php
$timestamp=1486830234542;
echo date('Y-m-d H:i:s', $timestamp/1000);
?>

我发现这个对话中的信息非常有用,所以我只想补充一下我是如何使用MySQL数据库中的时间戳和一点PHP来计算它的

 <?= date("Y-m-d\TH:i:s\+01:00",strtotime($column['loggedin'])) ?>

输出为:2017-03-03T08:22:36+01:00

非常感谢,Stewe,你的回答让我顿悟。