out of frustration of needing and not having strptime() on certain machines 
[read: windows]
and on certain versions of php [read: pre5.1] I wrote a crude userland 'compat' 
function.

it works in so far as I have tested it... but it feels like a crap 
implementation,
my attempt is below, the question can anyone else to else do better (or has 
anyone already
done so?) - bare in mind that the real php strptime() is capable of producing 
very different
bullshit than mine is [if you want to see try this: 
var_dump(strptime("23:28:00", "%H:%M:00"))]:

if (!function_exists('strptime')) {
    function strptime($str, $fmt)
    {
        $rtn = array(
            'tm_sec'    => 0,
            'tm_min'    => 0,
            'tm_hour'   => 0,
            'tm_mday'   => 0,
            'tm_mon'    => 0,
            'tm_year'   => 0,
            'tm_wday'   => 0,
            'tm_yday'   => 0,
            'unparsed'  => ''
        );

        $valueBits  = preg_split('#\W+#', $str);

        $formatBits = array();
        preg_match_all('#%[^%]#', $fmt, $formatBits);

        foreach ($valueBits as $k => $v) {
            if (!$v || !isset($formatBits[0][$k])) continue;            

                switch ($formatBits[0][$k]) {
                    case '%d':
                    case '%e':          
                    $rtn['tm_mday'] = intval($v);
                    break;

                    case '%m':
                    $rtn['tm_mon'] = intval($v);
                    break;

                    case '%Y':
                    case '%y':
                    $i = intval($v);
                        if ($i < 100/*strlen(trim($v)) == 2*/)
                        $i += ($i > 29) ? 1900 : 2000;                          
                

                    $rtn['tm_year'] = $i;
                    break;              

                    case '%b':
                    case '%B':
                    /*
                    // ER?? how to turn the month name
                    // (given in locale X) back into a month number?
                    // can't figure that out in less than 30 seconds,   
                    // so forget about it :-/
                    $rtn['tm_mon'] = ??;
                    //*/
                    break;
                
                    case '%H':
                    case '%I':
                    case '%k':
                    case '%l':
                    $rtn['tm_hour'] = intval($v);
                    break;

                    case '%P':
                    case '%p':
                    if (stristr($v, 'pm')) {
                        $rtn['tm_hour'] += 12;
                    } else if (stristr($v, 'am')) {
                        $rtn['tm_hour'] -= 12;
                    }
                    break;

                    case '%M':
                    $rtn['tm_min'] = intval($v);
                    break;
                }

        }

        return $rtn;
    }
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to