SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing XML dates

    Does anyone have any code they can share for parsing an XML input in the following format

    Code:
    <Time>2007-01-13T07:37:32Z</Time>
    so that it can be stored in a MySQL "DATETIME" field.

    Regards
    BBBS

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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($date112);
        
    $min = (int) substr($date142);
        
    $sec = (int) substr($date172);
        
    $mon = (int) substr($date52);
        
    $day = (int) substr($date82);
        
    $yea = (int) substr($date04);
        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!

  3. #3
    SitePoint Evangelist ldivinag's Avatar
    Join Date
    Jan 2005
    Location
    N37 33* W122 3*
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    notepad_coder,

    just curious as to this syntax:
    Code:
    $hou = (int) substr($date, 11, 2);
    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?

    i know about SETTYPE...

    thanks...
    leo d.

  4. #4
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's called Type Casting, you can use:
    Code:
    (int) or (integer) // integer
    (bool) or (boolean) // boolean
    (float), (double) or (real) // float
    (string) // string
    (array) // array
    (object) // object
    I can't really describe it, PHP.net say it like this:
    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.
    I learned it in C and use it in PHP. There are some things that only work when using (int), example:
    PHP 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] 
    Here's the link for php.net: http://us.php.net/manual/en/language...es.typecasting
    - the lid is off the maple syrup again!

  5. #5
    SitePoint Evangelist superuser2's Avatar
    Join Date
    Aug 2006
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simply put: type casting defines the "type" of the variable you are setting.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •