使用PHP,我想将UNIX时间戳转换为类似于
2008-07-17T09:24:17Z
的日期字符串。如何将时间戳1333699439
转换为2008-07-17T09:24:17Z
?2008-07-17T09:24:17Z
的日期字符串。如何将时间戳1333699439
转换为2008-07-17T09:24:17Z
?使用日期函数 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
以下是日期函数支持的一些格式
<?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
?>
设置默认时区非常重要,以获得正确的结果。
<?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);
?>
<?php
$timestamp=1486830234542;
echo date('Y-m-d H:i:s', $timestamp/1000);
?>
date_default_timezone_set("Asia/Tehran");
。 - Ali$dt = new DateTime('@1333699439');
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('c');
$unixtime_to_date = date('jS F Y h:i:s A (T)', $unixtime);
您可以像这样做......
$originalDate = "1585876500";
echo $newDate = date("Y-m-d h:i:sa", date($originalDate));
大多数人不会阅读评论,而且在被接受的答案中存在gmdate()
的问题,它返回的是GMT时间,因此如果我们使用date_default_timezone_set(zone)
就行不通了,应该使用date()
。
<?php
$ts = 1664706166;
$date = date('Y-m-d H:i:s', $ts);
$gmdate = gmdate('Y-m-d H:i:s', $ts);
echo "date() : $date\n";
echo "gmdate(): $gmdate\n\n";
date_default_timezone_set("Asia/Jakarta");
$date = date('Y-m-d H:i:s', $ts);
$gmdate = gmdate('Y-m-d H:i:s', $ts);
echo "date() : $date\n";
echo "gmdate(): $gmdate <- GMT\n";
/* result
date() : 2022-10-02 10:22:46
gmdate(): 2022-10-02 10:22:46
After set timezone
date() : 2022-10-02 17:22:46
gmdate(): 2022-10-02 10:22:46 <- GMT
*/
我在这次交谈中找到的信息非常有帮助,我想补充一下,是通过使用我的MySQL数据库中的时间戳和一点PHP来解决问题的。
<?= date("Y-m-d\TH:i:s\+01:00",strtotime($column['loggedin'])) ?>
gmdate()
返回的是格林威治标准时间下的日期/时间。date()
则会返回本地日期/时间。 - Matt Kdate
和gmdate
的输出将根据TZ变量而异(至少在*nix上)。 - Gert van den Berg