Reputation: 33070
In my PHP program, I'm using $_SERVER
to log the page's date visited:
$dateStamp = $_SERVER['REQUEST_TIME'];
The result is that the $dateStamp
variable contains a Unix timestamp like:
1385615749
What's the simplest way to convert it into a human-readable date/time (with year, month, day, hour, minutes, seconds)?
Upvotes: 15
Views: 34213
Reputation: 27854
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');
Upvotes: 36
Reputation: 7762
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
Upvotes: 2
Reputation:
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";
?>
Upvotes: 1
Reputation:
<?php
$date = new DateTime();
$dateStamp = $_SERVER['REQUEST_TIME'];
$date->setTimestamp($dateStamp);
echo $date->format('U = Y-m-d H:i:s') . "\n";
?>
Upvotes: 1
Reputation: 22711
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');
Upvotes: 0
Reputation:
this code will work for you
$dateStamp = $_SERVER['REQUEST_TIME'];
echo date('d-M-Y H:i:s',strtotime($dateStamp));
Upvotes: 0