开发者

php convert timestamp to xml datetime

开发者 https://www.devze.com 2023-02-07 05:57 出处:网络
In PHP, how do you convert a mysql timestamp like this \"1125443836\" to an xml/date-time like this:

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


'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".


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.


<?php echo '<wp:post_date>' . date('Y-m-d G:i:s',strtotime($yourMysqlDateStampVar)) . '</wp:post_date>';
0

精彩评论

暂无评论...
验证码 换一张
取 消