How to get xmldate from MySql date in php?
This post converting the MySql date format to xml date time format in php.
1. Code:
<?php
function convertMySqlDateToXmlDate($mySqlDate, $gmtOffset = "+00:00"){
$d = mySqlStringDate2String($mySqlDate, "Y-m-d");
$t = mySqlStringDate2String($mySqlDate, "H:i:s");
return $d . "T" . $t . $gmtOffset;
}
function mySqlStringDate2String($mysqlDateStr, $format){
if (is_null($mysqlDateStr) || trim($mysqlDateStr) == '' || "0000-00-00 00:00:00" == trim($mysqlDateStr)) {
return null;
}
try {
$d = date_create_from_format('Y-m-d H:i:s', $mysqlDateStr);
if (false === $d) {
throw new \Exception ("Invalid date");
}
}catch (\Exception $e) {
return $mysqlDateStr;
}
$str = $d->format($format);
return (false != $str) ? $str : null;
}
$str = "2018-11-15 00:00:00";
echo(convertMySqlDateToXmlDate($str));
?>
2. Example:
Input:
"2018-11-15 00:00:00"
Output:
2018-11-15T00:00:00+00:00