Kynian
Kynian

Reputation: 670

Trying to convert from seconds to date

Okay so I have an array of results from a table, we have a start and end time we are retrieving. the start/end time would look something like this:

1345497551   

Now I'm trying to convert this to a real time, for instance 1345497551 might become 2012/05/09 17:23:12 or something. I've found a few things but none seem to work correctly. one solution I tried, according to what someone was saying on another question on here, was

$createdate = date('H:i:s',$numberofsecs);  

where $numberofsecs was the time pulled in from the array. but this only ever outputs 17:00:00 repeatedly for every time we had available for testing.

How can I go about making this work correctly?

Upvotes: 15

Views: 39999

Answers (2)

Pragnesh Chauhan
Pragnesh Chauhan

Reputation: 8476

I have tried below code:

$ts = 1345497551;
$date = new DateTime("@$ts");
echo $date->format('U = Y-m-d H:i:s');

output : 1345497551 = 2012-08-20 21:19:11

Upvotes: 5

ernie
ernie

Reputation: 6356

Assuming that that's a standard unix timestamp string (seconds since midnight 1/1/1970), then you should be able to use date as you mentioned, but just modify the format string:

echo date('Y/m/d H:i:s', $numberofsecs);

The example you mention where you were always getting 17:00:00 could have been because your test cases were all only datestamps, encoded as timestamps, and having an offset from GMT . . .

Upvotes: 19

Related Questions