如何将日期时间转换为unix命令日期

问题描述:

How do I convert 2014-10-10 04:13:24 to 2014-10-10T04:13:24+00:00 in php or mysql

The above mentioned date is for xml format in sitemaps

For reference check Q: How do I compute lastmod date? (Sitemaps.org FAQ)

code which i have tried:

    echo "GIVEN DATE ".$timestamp = "2014-10-10 04:13:24";
echo "<br>";

$year = date('Y',strtotime($timestamp)).";
$month = date('m',strtotime($timestamp)).";
$day = date('d',strtotime($timestamp))."";
echo '<br>';
$hour = date('H',strtotime($timestamp))."";
$minutes = date('i',strtotime($timestamp))."";
$seconds = date('s',strtotime($timestamp))."<br>";


$gmktime= gmmktime($hour,$minutes,$seconds,$month,$day,$year)."<br>";

echo "output date".$isodate = date('c', $gmktime);

is the above out put conversion correct?

**OUTPUT**

GIVEN DATE : 2014-10-10 04:13:24

output date : 2014-10-10T06:13:24+02:00

如何将 2014-10-10 04:13:24 strong>转换为 2014-10-10T04:13:24 + 00:00 strong>在php或mysql中 p>

上述日期适用于站点地图中的xml格式 p> \ n

供参考检查问:如何计算lastmod日期? (Sitemaps.org常见问题解答) p>

我尝试过的代码: p>

  echo“GIVEN DATE”。$ timestamp =  “2014-10-10 04:13:24”; 
echo“&lt; br&gt;”; 
 
 $ year = date('Y',strtotime($ timestamp))。“; 
 $ month = date  ('m',strtotime($ timestamp))。“; 
 $ day = date('d',strtotime($ timestamp))。”“; 
echo'&lt; br&gt;'; 
 $ hour = date  ('H',strtotime($ timestamp))。“”; 
 $ minutes = date('i',strtotime($ timestamp))。“”; 
 $ seconds = date('s',strtotime($ 时间戳))。“&lt; br&gt;”; 
 
 
 $ gmktime = gmmktime($ hour,$ minutes,$ seconds,$ month,$ day,$ year)。“&lt; br&gt;”; 
  
echo“输出日期”。$ isodate = date('c',$ gmktime); 
  code>  pre> 
 
 

上面的输出转换是否正确? p> \ n

  ** OUTPUT ** 
 
日期:2014-10-10 04:13:24 
 
输出日期:2014-10-10T06:13:24  +02:00 
  code>  pre> 
  blockquote> 
  div>

PHP 5:

<?php
// Set the default timezone
date_default_timezone_set('UTC');


// Get Unix timestamp for a date
// Reference: mktime(hour, minute, second, month, day, year)
$time = mktime(04, 13, 24, 10, 10, 2014);

// Alternatively (must be a valid English date format)
$time = strtotime('2014-10-10 04:13:24');


// ISO 8601 date (added in PHP 5)
$isodate = date('c', $time);

echo $isodate; // 2014-10-10T04:13:24+00:00


// RFC 2822 formatted date
$rfcdate = date('r', $time);

echo $rfcdate; // Fri, 10 Oct 2014 04:13:24 +0000
?>

References:

In PHP

In PHP you use strptime() to parse the time and turn it into a structured array. Then pass the results of that into the mktime() function to get a UNIX timestamp.

In MySQL

Use UNIX_TIMESTAMP

SELECT UNIX_TIMESTAMP(datetime_column) FROM table;

Your output is correct in the light of the Sitemaps.org spec. "2014-10-10T06:13:24+02:00" is the same date/time as "2014-10-10T04:13:24+00:00".

Learn more about the W3C Datetime encoding which is used by Sitemaps.org.

Also, don't solve this with date functions, solve this with string function / operations: You change a single byte inside a string at a fixed position and then you append a string:

$timestamp = "2014-10-10 04:13:24";
$timestamp[10] = "T";
$timestamp .= "+00:00";

echo $timestamp, "
"; // 2014-10-10T04:13:24+00:00

Or if you like to save some bytes in your file use "Z" instead of "+00:00" to denote the timezone:

$timestamp = "2014-10-10 04:13:24";
$timestamp[10] = "T";
$timestamp .= "Z";

echo $timestamp, "
"; // 2014-10-10T04:13:24Z