user4951
user4951

Reputation: 33070

How to create human readable time stamp?

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

Answers (6)

Havenard
Havenard

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

Roopendra
Roopendra

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

Working Demo

Upvotes: 2

user3041094
user3041094

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

user3041407
user3041407

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

Krish R
Krish R

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

user2068607
user2068607

Reputation:

this code will work for you

$dateStamp = $_SERVER['REQUEST_TIME'];

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

Upvotes: 0

Related Questions