Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 986
NealWalters Avatar asked Oct 26 '25 05:10

NealWalters


2 Answers

'Y-m-d G:i:s' is not it. At least not in 2015 (I realize this is old).

In php 5.2.0 the DateTime class has a constant for this:

echo 'XML time: ' . date( DateTime::RFC3339, time() ) . '<br>';

DateTime::RFC3339 is set to "Y-m-d\TH:i:sP".

like image 126
drobert Avatar answered Oct 27 '25 19:10

drobert


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.

like image 33
Ali Rankine Avatar answered Oct 27 '25 20:10

Ali Rankine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!