2008/02/13 08:43

ISO 8601 format time format string conversion


When communicating with XML, ISO 8601 Format time string is used. In case of using xmlrpc, it contains ISO 8601 Format time string conversion module such as iso8601_encode, and ISO8601_decode.

If you don't use xmlrpc module, you can declare below module in order to use ISO 8601 Format time string conversion module. Of course, if your PHP supports the function, you don't need to declare  below functions.

// date helpers
function iso8601_encode($timet, $utc=0) {
    // return an ISO8601 encoded string
    // really, timezones ought to be supported
    // but the XML-RPC spec says:
    //
    // "Don't assume a timezone. It should be specified by the server in its
  // documentation what assumptions it makes about timezones."
    //
    // these routines always assume localtime unless
    // $utc is set to 1, in which case UTC is assumed
    // and an adjustment for locale is made when encoding
    if (!$utc) {
        $t=strftime("%Y%m%dT%H:%M:%S", $timet);
    } else {
        if (function_exists("gmstrftime"))
            // gmstrftime doesn't exist in some versions
            // of PHP
            $t=gmstrftime("%Y%m%dT%H:%M:%S", $timet);
        else {
            $t=strftime("%Y%m%dT%H:%M:%S", $timet-date("Z"));
        }
    }
    return $t;
}

function ISO8601_decode($idate, $utc=0) {
    // return a timet in the localtime, or UTC
    $t=0;
    if (ereg("([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})",
                     $idate, $regs)) {
        if ($utc) {
            $t=gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
        } else {
            $t=mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
        }
    }
    return $t;
}


As I mentioned, above functions will be very useful if you need to implement RSS, or Sitemap.
Trackback 4 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject Furniture stores.

    Tracked from Furniture stores in sydney nova scotia. 2009/01/06 09:20 delete

    Furniture stores in michigan. Furniture stores tn. Furniture stores and buford ga. Furniture stores in oahu. Furniture stores.

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/28 23:56 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 08:20 delete

    moneyideas

  4. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:43 delete

    moneyideas