php 如何将 Unix 时间戳转换回时间?

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

How can I convert a Unix timestamp back to time?

phptimeunix-timestamp

提问by esafwan

I have the following Unix timestamps.

我有以下 Unix 时间戳。

1301982430 1301982430 1301981474

1301981466 1301981466 1301981066

1301981058 1301981058 1301980388

1301980373 1301980373 1301979082

1301978478 1301978478 1301978478

1301982430 1301982430 1301981474

1301981466 1301981466 1301981066

1301981058 1301981058 1301980388

1301980373 1301980373 1301979082

1301978478 1301978478 1301978478

How do I convert it back to time that's human friendly?

我如何将其转换回人类友好的时间?

This doesn't seem to work,

这似乎不起作用,

strtotime($item->timestamp);

回答by Shakti Singh

You can use the php date function to get the date and time.

您可以使用php date 函数来获取日期和时间。

echo date('Y-m-d h:i:s',$item->timestamp);

回答by ThiefMaster

Use date()or strftime.

使用date()strftime

echo strftime("%Y-%m-%d, %H:%M:%S", time());
echo date('Y-m-d, H:i:s');

回答by Luca Fagioli

Use the datefunction.

使用日期函数。

date($format, $timestamp)

As stated:

就像声明的那样:

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

返回根据给定格式字符串格式化的字符串,使用给定的整数时间戳或当前时间(如果没有给定时间戳)。换句话说,timestamp 是可选的,默认为 time() 的值。

Some format examples:

一些格式示例:

$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 Gabriel Solomon

datefunction

日期函数

echo date(DATE_ATOM, '1301980373');

回答by Shankar Damodaran

Make use of the DateTimeClass.

使用DateTime类。



   <?php
    $dt = new DateTime();
    $dt->setTimestamp(160000); //<--- Pass a UNIX TimeStamp
    echo $dt->format('H:i'); //"prints" 20:26

回答by kahriman

echo date('Y-m-d H:i:s',$item->timestamp);

Is perhaps better, because the hour has to be capital!

也许更好,因为时间必须是资本!