DateTime::createFromFormat in PHP < 5.3.0

后端 未结 5 544
甜味超标
甜味超标 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: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);
    }
    

提交回复
热议问题