php 将 UNIX 时间戳转换为格式化日期字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10040291/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:12:10  来源:igfitidea点击:

Converting a UNIX Timestamp to Formatted Date String

phpunix-timestamp

提问by Utku Dalmaz

Using PHP, I want to convert UNIX timestamps to date strings similar to this: 2008-07-17T09:24:17Z

使用 PHP,我想将 UNIX 时间戳转换为类似于此的日期字符串: 2008-07-17T09:24:17Z

How do I convert a timestamp such as 1333699439to 2008-07-17T09:24:17Z?

如何转换的时间戳,如13336994392008-07-17T09:24:17Z

回答by stewe

Try gmdatelike this:

gmdate像这样尝试:

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

回答by Sandeep Manne

use date function date ( string $format [, int $timestamp = time() ] )

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

Use date('c',time())as format to convert to ISO 8601 date (added in PHP 5) - 2012-04-06T12:45:47+05:30

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

use date("Y-m-d\TH:i:s\Z",1333699439)to get 2012-04-06T13:33:59Z

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

Here are some of the formats date function supports

以下是日期函数支持的一些格式

<?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
?>

回答by liquorvicar

Assuming you are using PHP5.3 then the modern way of handling dates is via the native DateTime class. To get the current time you can just call

假设您使用的是 PHP5.3,那么处理日期的现代方法是通过本机DateTime 类。要获取当前时间,您只需致电

$currentTime = new DateTime();

To create a DateTime object from a specific timestamp (i.e. not now)

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

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

To get a formatted string you can then call

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

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

See the manual page here

请参阅此处手册页

回答by klit67

It is very important to set a default timezone to get the correct result

设置默认时区以获得正确结果非常重要

<?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);
?> 

Online conversion help: http://freeonlinetools24.com/timestamp

在线转换帮助:http: //freeonlinetools24.com/timestamp

回答by sunil

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

回答by Linus Gudn Jokela

$unixtime_to_date = date('jS F Y h:i:s A (T)', $unixtime);

This should work to.

这应该工作。

回答by Samuel Ramzan

I found the information in this conversation so helpful that I just wanted to add how I figured it out by using the timestamp from my MySQL database and a little PHP

我发现此对话中的信息非常有用,因此我只想通过使用我的 MySQL 数据库中的时间戳和一些 PHP 来补充我是如何计算出来的

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

The output was: 2017-03-03T08:22:36+01:00

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

Thanks very much Stewe you answer was a eureka for me.

非常感谢 Stewe 你的回答对我来说是一个惊喜。