将时间戳转换为可读的日期/时间 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5213528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert timestamp to readable date/time PHP
提问by Adamski
I have a timestamp stored in a session (1299446702).
我有一个存储在会话中的时间戳 (1299446702)。
How can I convert that to a readable date/time in PHP? I have tried srttotime, etc. to no avail.
如何在 PHP 中将其转换为可读的日期/时间?我试过 srttotime 等都无济于事。
回答by Rocket Hazmat
回答by Explosion Pills
strtotime makes a date string into a timestamp. You want to do the opposite, which is date. The typical mysql date format is date('Y-m-d H:i:s');
Check the manual page for what other letters represent.
strtotime 将日期字符串转换为时间戳。你想做相反的事情,即date。典型的 mysql 日期格式是date('Y-m-d H:i:s');
检查手册页以了解其他字母代表什么。
If you have a timestamp that you want to use (apparently you do), it is the second argument of date()
.
如果您有要使用的时间戳(显然您这样做),则它是date()
.
回答by Razan Paul
I just added H:i:s to Rocket's answer to get the time along with the date.
我刚刚在 Rocket 的回答中添加了 H:i:s 以获取时间和日期。
echo date('m/d/Y H:i:s', 1299446702);
Output: 03/06/2011 16:25:02
输出:03/06/2011 16:25:02
回答by Stanislav Malomuzh
$timestamp = 1465298940;
$datetimeFormat = 'Y-m-d H:i:s';
$date = new \DateTime();
// If you must have use time zones
// $date = new \DateTime('now', new \DateTimeZone('Europe/Helsinki'));
$date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
result: 2016-06-07 14:29:00
结果:2016-06-07 14:29:00
Other time zones:
其他时区:
回答by Rahul Dadhich
If you are using PHP date()
, you can use this code to get the date, time, second, etc.
如果您使用的是 PHP date()
,则可以使用此代码获取日期、时间、秒等。
$time = time(); // you have 1299446702 in time
$year = $time/31556926 % 12; // to get year
$week = $time / 604800 % 52; // to get weeks
$hour = $time / 3600 % 24; // to get hours
$minute = $time / 60 % 60; // to get minutes
$second = $time % 60; // to get seconds
回答by Pascal
I know that's an old one question, but its high in the search results.
我知道这是一个古老的问题,但它在搜索结果中很高。
If anyone wants timestamp conversion directly to a DateTime object, there's a simple one-liner:
如果有人想要将时间戳直接转换为 DateTime 对象,则有一个简单的单行:
$timestamp = 1299446702;
$date = DateTime::createFromFormat('U', $timestamp);
Following @sromero comment, timezone parameter (the 3rd param in DateTime::createFromFormat()) is ignored when unix timestamp is passed, so the below code is unnecessary.
在@sromero 注释之后,当传递 unix 时间戳时,时区参数(DateTime::createFromFormat() 中的第三个参数)将被忽略,因此下面的代码是不必要的。
$date = DateTime::createFromFormat('U', $timestamp, new DateTimeZone('UTC'); // not needed, 3rd parameter is ignored
You may check PHP's manual for DateTime::createFromFormatfor more info and options.
您可以查看 PHP 的DateTime::createFromFormat手册以获取更多信息和选项。
回答by Neil Dhakal
Try it.
尝试一下。
<?php
$timestamp=1333342365;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
回答by Pratik Mehta
You can try this:
你可以试试这个:
$mytimestamp = 1465298940;
echo gmdate("m-d-Y", $mytimestamp);
Output :
输出 :
06-07-2016
06-07-2016
回答by bishop
Unless you need a custom date and time format, it's easier, less error-prone, and more readable to use one of the built-in date time format constants:
除非您需要自定义日期和时间格式,否则使用内置日期时间格式常量之一更容易、更不容易出错且更具可读性:
echo date(DATE_RFC822, 1368496604);
回答by Sunni Ha
Try this one:
试试这个:
echo date('m/d/Y H:i:s', 1541843467);