DateTime::createFromFormat in PHP < 5.3.0

后端 未结 5 509
甜味超标
甜味超标 2021-01-20 04:06

I\'m looking for a function identical to DateTime::createFromFormat but I need it to work in an environment running a version of PHP which is older than v5.3. Basically I ne

相关标签:
5条回答
  • 2021-01-20 04:15

    For PHP version prior to 5.3 I use this function bellow to get DateTime object form strings formated as 'DD.MM.YYYY'.

    function ParseForDateTimeValue ($strText)
    {
        if ($strText != "")
        {
            if (ereg("^([0-9]{1,2})[/\.]\s*([0-9]{1,2})[/\.]\s*([0-9]{2,4})$",$strText,$arr_parts)) {
    
                $month = ltrim($arr_parts[2], '0');
                $day = ltrim($arr_parts[1], '0');
                $year = $arr_parts[3];
    
                if (checkdate($month, $day, $year)) {
                   return new DateTime(date('Y-m-d H:i:s', mktime(0, 0, 0, $month, $day, $year));
                }
            }
        }
        return NULL;
    }
    
    • First validate if input is correct.
    • Then extract month, day and year
    • Use checkdate to validate the date.
    • Convert it to timestamp and pass it to contructor of DateTime and return it.
    0 讨论(0)
  • 2021-01-20 04:17

    You can use Zend_Date class from Zend Framework: http://framework.zend.com/manual/en/zend.date.html

    $date = new Zend_Date($string, $format);
    $timestamp = $date->get();
    
    0 讨论(0)
  • 2021-01-20 04:22

    You can create a timestamp and then run it through date.. to create the timestamp you can do some explode on the string you get..

    $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
    echo "Tomorrow is ".date("Y/m/d", $tomorrow);
    
    0 讨论(0)
  • 2021-01-20 04:25

    DateTime::createFromFormat and date_parse_from_format have been added in PHP 5.3 because there was a high demand for that feature, especially from developpers who code for users who don't use US date/time formats.


    Before those, you had to develop a specific function to parse the format you were using ; with PHP < 5.3, what is generally done is :

    • Decide which format will be accepted by the application
    • Display some message saying something like "your input should be JJ/MM/AAAA" (French for DD/MM/YYYY)
    • Check that the input is OK, regarding to that format
    • And parse it to convert it to a date/time that PHP can understand.

    Which means applications and developpers generally didn't allow for that many formats, as each format meant one different additionnal validation+parsing function.


    If you really need that kind of function, that allows for any possible format, I'm afraid you'll kind of have to write it yourself :-(

    Maybe taking a look at the sources of date_parse_from_format could help, if you understand C code ? It should be in something like ext/date/php_date.c -- but doesn't seem to be that simple : it's calling the timelib_parse_from_format function, which is defined in ext/data/lib/parse_date.c, and doesn't look that friendly ^^

    0 讨论(0)
  • 2021-01-20 04:42

    You may use strptime() - it's using strftime() format. A bit different compared to date() format but does almost the same things.

    function createFromFormat($strptimeFormat, $date) {
        $date = strptime($date, $strptimeFormat);
        $date['tm_year'] += 1900;
        $date['tm_mon']++;
        $timestamp = mktime($date['tm_hour'], $date['tm_min'], $date['tm_sec'], $date['tm_mon'], $date['tm_mday'], $date['tm_year']);
        return new DateTime('@'. $timestamp);
    }
    
    0 讨论(0)
提交回复
热议问题