How to create human readable time stamp?

感情迁移 提交于 2019-11-28 03:07:11

问题


This is my current PHP program:

$dateStamp = $_SERVER['REQUEST_TIME'];

which I later log.

The result is that the $dateStamp variable contains numbers like:

1385615749

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

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.


回答1:


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

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.

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



回答2:


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




回答3:


<?php
$date = new DateTime();

$dateStamp = $_SERVER['REQUEST_TIME'];

$date->setTimestamp($dateStamp);

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



回答4:


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



回答5:


this code will work for you

$dateStamp = $_SERVER['REQUEST_TIME'];

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



回答6:


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

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

OR

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


来源:https://stackoverflow.com/questions/20258294/how-to-create-human-readable-time-stamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!