php 如何创建人类可读的时间戳?

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

How to create human readable time stamp?

phptimestampunix-timestamp

提问by user4951

This is my current PHP program:

这是我当前的 PHP 程序:

$dateStamp = $_SERVER['REQUEST_TIME'];

which I later log.

我后来登录。

The result is that the $dateStampvariable contains numbers like:

结果是$dateStamp变量包含如下数字:

1385615749

1385615749

This is a Unix timestamp, but I want it to contain human readable date with hour, minutes, seconds, date, months, and years.

这是一个 Unix 时间戳,但我希望它包含人类可读的日期,包括小时、分钟、秒、日期、月和年。

So I need a function that will convert it into a human readable date.

所以我需要一个函数将它转换成人类可读的日期。

How would I do that?

我该怎么做?

There are other similar questions but not quite like this. I want the simplest possible solution.

还有其他类似的问题,但不太像这样。我想要最简单的解决方案。

回答by Havenard

This number is called Unix time. Functions like date()can accept it as the optional second parameter to format it in readable time.

这个数字称为Unix 时间。像这样的函数date()可以接受它作为可选的第二个参数,以便在可读的时间内对其进行格式化。

Example:

例子:

echo date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);

If you omit the second parameter the current value of time()will be used.

如果省略第二个参数,time()将使用的当前值。

echo date('Y-m-d H:i:s');

回答by Roopendra

Your functional approch to convert timestamp into Human Readable format are as following

您将时间戳转换为人类可读格式的功能方法如下

function convertDateTime($unixTime) {
   $dt = new DateTime("@$unixTime");
   return $dt->format('Y-m-d H:i:s');
}

$dateVarName = convertDateTime(1385615749);

echo $dateVarName;

Output :-

输出 :-

2013-11-28 05:15:49

Working Demo

Working Demo

回答by Roopendra

<?php
$date = new DateTime();

$dateStamp = $_SERVER['REQUEST_TIME'];

$date->setTimestamp($dateStamp);

echo $date->format('U = Y-m-d H:i:s') . "\n";
?>

回答by Roopendra

you can try this

你可以试试这个

<?php
$date = date_create();
$dateStamp = $_SERVER['REQUEST_TIME'];
date_timestamp_set($date, $dateStamp);
echo date_format($date, 'U = D-M-Y H:i:s') . "\n";
?>

回答by Krish R

REQUEST_TIME- It is unix timestamp - The timestamp of the start of the request.

REQUEST_TIME- 它是 unix 时间戳 - 请求开始的时间戳。

$dateStamp = $_SERVER['REQUEST_TIME'];
echo date('d m Y', $dateStamp);

OR

或者

$date = new DateTime($dateStamp);
echo $date->format('Y-m-d');

回答by Krish R

this code will work for you

这段代码对你有用

$dateStamp = $_SERVER['REQUEST_TIME'];

echo date('d-M-Y H:i:s',strtotime($dateStamp));