NealWalters NealWalters - 4 months ago 16
PHP Question

php convert timestamp to xml datetime

In PHP, how do you convert a mysql timestamp like this "1125443836"
to an xml/date-time like this:

<wp:post_date>2011-01-25 02:10:32</wp:post_date>


UPDATE:
The columns in the mySQL are stored as int(10).

Based on samples below, this is what I tried with two sample values in my database.
Something is wrong, maybe hash or Unix dates stored in mySQL table?

$testSqlDateStamp = "1125443836";
echo "<BR>DateTest=".date('Y-m-d G:i:s',strtotime($testSqlDateStamp));

$testSqlDateStamp = "1125444107";
echo "<BR>DateTest=".date('Y-m-d G:i:s',strtotime($testSqlDateStamp));
echo "<BR>";


Results:

DateTest=1969-12-31 18:00:00
DateTest=1969-12-31 18:00:00


Second Update: worked without calling strtotime

$testSqlDateStamp = "1125444107";
echo "<BR>DateTest=".date('Y-m-d G:i:s',$testSqlDateStamp);
$testSqlDateStamp = "1125443836";
echo "<BR>DateTest=".date('Y-m-d G:i:s',$testSqlDateStamp);


Results:

DateTest=2005-08-30 18:21:47
DateTest=2005-08-30 18:17:16

Answer
date("Y-d-m G-i-s",$time); 

That should do it.

http://php.net/manual/en/function.date.php

EDIT: this won't work for mySQL timestamps, they must be converted to unix timestamps via strtotime() info: PHP: strtotime - Manual.