How to create a human timestamp?

This is my current PHP program:

$dateStamp = $_SERVER['REQUEST_TIME']; 

which I record later.

As a result, the $dateStamp variable contains numbers like:

1385615749

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

Therefore, I need a function that converts it into a human-readable date.

How can I do it?

There are other similar questions, but not quite so. I want the simplest possible solution.

+6
source share
6 answers

This number is called Unix time . Functions such as date() can take it as an optional second parameter to format it in a matter of time.

Example:

 echo date('Ymd H:i:s', $_SERVER['REQUEST_TIME']); 

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

 echo date('Ymd H:i:s'); 
+19
source

Your functional way to convert a timestamp to a human readable format is as follows

 function convertDateTime($unixTime) { $dt = new DateTime("@$unixTime"); return $dt->format('Ymd H:i:s'); } $dateVarName = convertDateTime(1385615749); echo $dateVarName; 

Output: -

 2013-11-28 05:15:49 

Working demo

+2
source
 <?php $date = new DateTime(); $dateStamp = $_SERVER['REQUEST_TIME']; $date->setTimestamp($dateStamp); echo $date->format('U = Ymd H:i:s') . "\n"; ?> 
+1
source

you can try this

 <?php $date = date_create(); $dateStamp = $_SERVER['REQUEST_TIME']; date_timestamp_set($date, $dateStamp); echo date_format($date, 'U = DMY H:i:s') . "\n"; ?> 
+1
source

REQUEST_TIME - This is a unix timestamp - the timestamp of the start of the request.

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

OR

 $date = new DateTime($dateStamp); echo $date->format('Ym-d'); 
0
source

this code will work for you

 $dateStamp = $_SERVER['REQUEST_TIME']; echo date('dMY H:i:s',strtotime($dateStamp)); 
0
source

Source: https://habr.com/ru/post/958971/


All Articles