Does anyone have any code they can share for parsing an XML input in the following format
so that it can be stored in a MySQL "DATETIME" field.Code:<Time>2007-01-13T07:37:32Z</Time>
Regards
BBBS
| SitePoint Sponsor |
Does anyone have any code they can share for parsing an XML input in the following format
so that it can be stored in a MySQL "DATETIME" field.Code:<Time>2007-01-13T07:37:32Z</Time>
Regards
BBBS
Hope this will help... I use this for Atom and RSS feeds. I'm assuming you know how to get the XML tag.
PHP Code:<?php
function formatDate($date, $get = "stamp") {
$hou = (int) substr($date, 11, 2);
$min = (int) substr($date, 14, 2);
$sec = (int) substr($date, 17, 2);
$mon = (int) substr($date, 5, 2);
$day = (int) substr($date, 8, 2);
$yea = (int) substr($date, 0, 4);
if ($get == "stamp") {
return mktime($hou, $min, $sec, $mon, $day, $yea);
} elseif ($get == "mysql") {
return $yea."-".sprintf("%'02s", $mon)."-".sprintf("%'02s", $day)." ".
sprintf("%'02s", $hou).":".sprintf("%'02s", $min).":".sprintf("%'02s", $sec);
}
}
$xml_date = "2007-04-06T04:26:12Z";
$date = formatDate($xml_date);
// returns 1175855172
$date = formatDate($xml_date, "mysql");
// returns 2007-04-06 04:26:12
?>
- the lid is off the maple syrup again!



notepad_coder,
just curious as to this syntax:
what does the (INT) do? i mean i understand it turns the result into an integer, but where in the PHP's documentation mention this?Code:$hou = (int) substr($date, 11, 2);
i know about SETTYPE...
thanks...
leo d.
It's called Type Casting, you can use:
I can't really describe it, PHP.net say it like this:Code:(int) or (integer) // integer (bool) or (boolean) // boolean (float), (double) or (real) // float (string) // string (array) // array (object) // object
I learned it in C and use it in PHP. There are some things that only work when using (int), example:Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.
Here's the link for php.net: http://us.php.net/manual/en/language...es.typecastingPHP Code:$month = array("last" => (int) $xml->last,
"current" => (int) $xml->current,
"next" => (int) $xml->next);
# $month is an array of timestamps $xml->last,
# $xml->current, and $xml->next;
# If i were to not use (int) I couldn't use date()
# on $month[$key]
- the lid is off the maple syrup again!
Simply put: type casting defines the "type" of the variable you are setting.
Bookmarks