Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:57:12+00:00 2026-06-15T10:57:12+00:00

I asked this question a while ago. Now I need to run it on

  • 0

I asked this question a while ago. Now I need to run it on production server, but the server is 5.2.9 …

How can I write an alternative function that would give me the exact result as the DateTime::createFromFormat function?

Yes, I know there are duplicates, but they all use strptime. But when I try to use it, I get: Fatal error: Call to undefined function strptime().

And no, I have no access on this production server, so I can’t change any settings for it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:57:14+00:00Added an answer on June 15, 2026 at 10:57 am

    Include this and you can use strptime:

    /*
     * This work of Lionel SAURON (http://sauron.lionel.free.fr:80) is licensed under the
     * Creative Commons Attribution 2.0 France License.
     *
     * To view a copy of this license, visit http://creativecommons.org/licenses/by/2.0/fr/
     * or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
     */
    
    /**
     * Parse a time/date generated with strftime().
     *
     * This function is the same as the original one defined by PHP (Linux/Unix only),
     *  but now you can use it on Windows too.
     *  Limitation : Only this format can be parsed %S, %M, %H, %d, %m, %Y
     * 
     * @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 <code>$sDate</code> parsed, or <code>false</code> on error.
     */
    if(function_exists("strptime") == false)
    {
        function strptime($sDate, $sFormat)
        {
            $aResult = array
            (
                'tm_sec'   => 0,
                'tm_min'   => 0,
                'tm_hour'  => 0,
                'tm_mday'  => 1,
                'tm_mon'   => 0,
                'tm_year'  => 0,
                'tm_wday'  => 0,
                'tm_yday'  => 0,
                'unparsed' => $sDate,
            );
    
            while($sFormat != "")
            {
                // ===== 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['unparsed'] = ($sFormat == $sDate) ? "" : $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['unparsed'] = $sDate;
    
                $sFormatCurrent = substr($sFormat, 0, 2);
                $sFormatAfter   = substr($sFormat, 2);
    
                $nValue = -1;
                $sDateAfter = "";
    
                switch($sFormatCurrent)
                {
                    case '%S': // Seconds after the minute (0-59)
    
                        sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
    
                        if(($nValue < 0) || ($nValue > 59)) return false;
    
                        $aResult['tm_sec']  = $nValue;
                        break;
    
                    // ----------
                    case '%M': // Minutes after the hour (0-59)
                        sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
    
                        if(($nValue < 0) || ($nValue > 59)) return false;
    
                        $aResult['tm_min']  = $nValue;
                        break;
    
                    // ----------
                    case '%H': // Hour since midnight (0-23)
                        sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
    
                        if(($nValue < 0) || ($nValue > 23)) return false;
    
                        $aResult['tm_hour']  = $nValue;
                        break;
    
                    // ----------
                    case '%d': // Day of the month (1-31)
                        sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
    
                        if(($nValue < 1) || ($nValue > 31)) return false;
    
                        $aResult['tm_mday']  = $nValue;
                        break;
    
                    // ----------
                    case '%m': // Months since January (0-11)
                        sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
    
                        if(($nValue < 1) || ($nValue > 12)) return false;
    
                        $aResult['tm_mon']  = ($nValue - 1);
                        break;
    
                    // ----------
                    case '%Y': // Years since 1900
                        sscanf($sDate, "%4d%[^\\n]", $nValue, $sDateAfter);
    
                        if($nValue < 1900) return false;
    
                        $aResult['tm_year']  = ($nValue - 1900);
                        break;
    
                    // ----------
                    default:
                        break 2; // Break Switch and while
    
                } // END of case format
    
                // ===== Next please =====
                $sFormat = $sFormatAfter;
                $sDate   = $sDateAfter;
    
                $aResult['unparsed'] = $sDate;
    
            } // END of while($sFormat != "")
    
            // ===== Create the other value of the result array =====
            $nParsedDateTimestamp = mktime($aResult['tm_hour'], $aResult['tm_min'], $aResult['tm_sec'],
                                    $aResult['tm_mon'] + 1, $aResult['tm_mday'], $aResult['tm_year'] + 1900);
    
            // Before PHP 5.1 return -1 when error
            if(($nParsedDateTimestamp === false)
            ||($nParsedDateTimestamp === -1)) return false;
    
            $aResult['tm_wday'] = (int) strftime("%w", $nParsedDateTimestamp); // Days since Sunday (0-6)
            $aResult['tm_yday'] = (strftime("%j", $nParsedDateTimestamp) - 1); // Days since January 1 (0-365)
    
            return $aResult;
        } // END of function
    
    } // END of if(function_exists("strptime") == false)
    

    However, since DateTime::createFromFormat returns an object of DateTime, you can’t get the exact same result in PHP < 5.3 (unless your willing to write your own class which behaves exactly like DateTime)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I realize that this question has been asked once or twice ago but
Okay so I know I asked a similar question a while ago, but this
This question is related to this initial question asked a little while ago. Now,
UPDATE 10/19/2010 I know I asked this question a while ago, but the workarounds
Update I asked this question quite a while ago now, and I was curious
I asked this question a while ago. I now know it is a Bad
I asked this question a while back but now I'm looking to implement an
This question was asked quite some time ago, and while it covers possible solutions
A while ago I asked this question about how to defer updates to an
I asked a similar question a while ago but I didn't know very much,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.