<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
	<channel>
		<title>lily-software.com : strptime() под windows. Парсим дату</title>
		<link>https://lily-software.com</link>
		<description>Последние сообщения в теме</description>
		<generator>Cotonti</generator>
		<language>ru</language>
		<pubDate>Sat, 02 May 2026 12:15:08 +0300</pubDate>

		<item>
			<title>Alex</title>
			<description><![CDATA[Недавно, занимаясь одним проектом, потребоваось <strong>спарсить строку, содержащую дату</strong> по заданному формату. Формат известен.<br />
<br />
В <strong>php</strong>, начиная с версии  <strong>5.1.0RC1</strong> есть функция для этих целей: <strong>strptime()</strong>, которая принимает в качестве параметров саму строку и формат. Строго говоря, эта функция является обратной к strftime().<br />
<br />
Но вот для Windows-платформ эта функция не реализована. Странно, но факт. <br />
<br />
Для счастливых обладателей &quot;окон&quot; предлагаю воспользоваться приведенной ниже реализацией данной функции. Для переносимости между платформами, сама функция заключена в условие if(function_exists(&quot;strptime&quot;) == false). Если Вы перенесете скрипт на UNIX хостинг, то этот условный оператор позволит избежать ошибки, связанной с повторным объявлением функции.<br />
<br />
сам код:<br />
<br />
<pre class="code">
/*
* Для совместимости с системами, где нет этой ф-ции. Преобразуем время, заданное соотвественно формату
*	strptime() returns an array with the date parsed, or FALSE on error. 
*
* Month and weekday names and other language dependent strings respect the current
* locale set with setlocale() (LC_TIME). 
*
* Функция уже есть в PHP PHP 5 &gt;= 5.1.0RC1. Замечание: Для Windows-платформ эта функция не реализована.
*
* Функция обратна функции: strtotime()
*
* @author Lionel SAURON 
* @version 1.0 
* @public 
*  
* @param $sDate(string)    The string to parse (e.g. returned from strftime()). 
* @param $sFormat(string)  The format used in date  (e.g. the same as used in strftime()). 
* @return (array)          Returns an array with the &lt;code&gt;$sDate&lt;/code&gt; parsed, or &lt;code&gt;false&lt;/code&gt; on error. 

Таблица 1. The following parameters are returned in the array

parameters Description 
tm_sec 		Seconds after the minute (0-61) 
tm_min 		Minutes after the hour (0-59) 
tm_hour 	Hour since midnight (0-23) 
tm_mday 	Day of the month (1-31) 
tm_mon 		Months since January (0-11) 
tm_year 	Years since 1900 
tm_wday 	Days since Sunday (0-6) 
tm_yday 	Days since January 1 (0-365) 
unparsed 	the date part which was not recognized using the specified format 

*/ 
if(function_exists(&quot;strptime&quot;) == false) 
{ 
    function strptime($sDate, $sFormat) 
    { 
        $aResult = array 
        ( 
            'tm_sec'   =&gt; 0, 
            'tm_min'   =&gt; 0, 
            'tm_hour'  =&gt; 0, 
            'tm_mday'  =&gt; 1, 
            'tm_mon'   =&gt; 0, 
            'tm_year'  =&gt; 0, 
            'tm_wday'  =&gt; 0, 
            'tm_yday'  =&gt; 0, 
            'unparsed' =&gt; $sDate, 
        ); 
         
        while($sFormat != &quot;&quot;) 
        { 
            // ===== Search a %x element, Check the static string before the %x ===== 
            $nIdxFound = strpos($sFormat, '%'); 
            if($nIdxFound === false) 
            { 
                 
                // There is no more format. Check the last static string. 
                $aResult&#091;'unparsed'&#093; = ($sFormat == $sDate) ? &quot;&quot; : $sDate; 
                break; 
            } 
             
            $sFormatBefore = substr($sFormat, 0, $nIdxFound); 
            $sDateBefore   = substr($sDate,   0, $nIdxFound); 
             
            if($sFormatBefore != $sDateBefore) break; 
             
            // ===== Read the value of the %x found ===== 
            $sFormat = substr($sFormat, $nIdxFound); 
            $sDate   = substr($sDate,   $nIdxFound); 
             
            $aResult&#091;'unparsed'&#093; = $sDate; 
             
            $sFormatCurrent = substr($sFormat, 0, 2); 
            $sFormatAfter   = substr($sFormat, 2); 
             
            $nValue = -1; 
            $sDateAfter = &quot;&quot;; 
            switch($sFormatCurrent) 
            { 
                case '%S': // Seconds after the minute (0-59) 
                     
                    sscanf($sDate, &quot;%2d%&#091;^\\n&#093;&quot;, $nValue, $sDateAfter); 
                     
                    if(($nValue &lt; 0) || ($nValue &gt; 59)) return false; 
                     
                    $aResult&#091;'tm_sec'&#093;  = $nValue; 
                    break; 
                 
                // ---------- 
                case '%M': // Minutes after the hour (0-59) 
                    sscanf($sDate, &quot;%2d%&#091;^\\n&#093;&quot;, $nValue, $sDateAfter); 
                     
                    if(($nValue &lt; 0) || ($nValue &gt; 59)) return false; 
                 
                    $aResult&#091;'tm_min'&#093;  = $nValue; 
                    break; 
                 
                // ---------- 
                case '%H': // Hour since midnight (0-23) 
                    sscanf($sDate, &quot;%2d%&#091;^\\n&#093;&quot;, $nValue, $sDateAfter); 
                     
                    if(($nValue &lt; 0) || ($nValue &gt; 23)) return false; 
                 
                    $aResult&#091;'tm_hour'&#093;  = $nValue; 
                    break; 
                 
                // ---------- 
                case '%d': // Day of the month (1-31) 
                    sscanf($sDate, &quot;%2d%&#091;^\\n&#093;&quot;, $nValue, $sDateAfter); 
                     
                    if(($nValue &lt; 1) || ($nValue &gt; 31)) return false; 
                 
                    $aResult&#091;'tm_mday'&#093;  = $nValue; 
                    break; 
                 
                // ---------- 
                case '%m': // Months since January (0-11) 
                    sscanf($sDate, &quot;%2d%&#091;^\\n&#093;&quot;, $nValue, $sDateAfter); 
                     
                    if(($nValue &lt; 1) || ($nValue &gt; 12)) return false; 
                 
                    $aResult&#091;'tm_mon'&#093;  = ($nValue - 1); 
                    break; 
                 
                // ---------- 
                case '%Y': // Years since 1900 
                    sscanf($sDate, &quot;%4d%&#091;^\\n&#093;&quot;, $nValue, $sDateAfter); 
                     
                    if($nValue &lt; 1900) return false; 
                 
                    $aResult&#091;'tm_year'&#093;  = ($nValue - 1900); 
                    break; 
                 
                // ---------- 
                default: break 2; // Break Switch and while 
            } 
             
            // ===== Next please ===== 
            $sFormat = $sFormatAfter; 
            $sDate   = $sDateAfter; 
             
            $aResult&#091;'unparsed'&#093; = $sDate; 
             
        } // END while($sFormat != &quot;&quot;) 
         
         
        // ===== Create the other value of the result array ===== 
        $nParsedDateTimestamp = mktime($aResult&#091;'tm_hour'&#093;, $aResult&#091;'tm_min'&#093;, $aResult&#091;'tm_sec'&#093;, 
                                $aResult&#091;'tm_mon'&#093; + 1, $aResult&#091;'tm_mday'&#093;, $aResult&#091;'tm_year'&#093; + 1900); 
         
        // Before PHP 5.1 return -1 when error 
        if(($nParsedDateTimestamp === false) 
        ||($nParsedDateTimestamp === -1)) return false; 
         
        $aResult&#091;'tm_wday'&#093; = (int) strftime(&quot;%w&quot;, $nParsedDateTimestamp); // Days since Sunday (0-6) 
        $aResult&#091;'tm_yday'&#093; = (strftime(&quot;%j&quot;, $nParsedDateTimestamp) - 1); // Days since January 1 (0-365) 

        return $aResult; 
    } // END of function 
     
} // END if(function_exists(&quot;strptime&quot;) == false) 
</pre>]]></description>
			<pubDate>вс, 12 апр 2009 10:34:33 +0300</pubDate>
			<link><![CDATA[https://lily-software.com/forums?m=posts&q=105&d=0#post466]]></link>
		</item>
	</channel>
</rss>