php dateTime::createFromFormat in 5.2? php dateTime::createFromFormat in 5.2? php php

php dateTime::createFromFormat in 5.2?


just include the next code

function DEFINE_date_create_from_format()  {function date_create_from_format( $dformat, $dvalue )  {    $schedule = $dvalue;    $schedule_format = str_replace(array('Y','m','d', 'H', 'i','a'),array('%Y','%m','%d', '%I', '%M', '%p' ) ,$dformat);    // %Y, %m and %d correspond to date()'s Y m and d.    // %I corresponds to H, %M to i and %p to a    $ugly = strptime($schedule, $schedule_format);    $ymd = sprintf(        // This is a format string that takes six total decimal        // arguments, then left-pads them with zeros to either        // 4 or 2 characters, as needed        '%04d-%02d-%02d %02d:%02d:%02d',        $ugly['tm_year'] + 1900,  // This will be "111", so we need to add 1900.        $ugly['tm_mon'] + 1,      // This will be the month minus one, so we add one.        $ugly['tm_mday'],         $ugly['tm_hour'],         $ugly['tm_min'],         $ugly['tm_sec']    );    $new_schedule = new DateTime($ymd);   return $new_schedule;  }}if( !function_exists("date_create_from_format") ) DEFINE_date_create_from_format();


Because strtotime does poorly when confronted with D/M/Y and date_create_from_format isn't available, strptime may be your only hope here. It does some pretty oldschool things, like deal with years as if they are the number of years since 1900 and deal with months as if January was month zero. Here's some horrible example code that uses sprintf to reassemble the date into something DateTime understands:

$schedule = '31/03/2011 01:22 pm';// %Y, %m and %d correspond to date()'s Y m and d.// %I corresponds to H, %M to i and %p to a$ugly = strptime($schedule, '%d/%m/%Y %I:%M %p');$ymd = sprintf(    // This is a format string that takes six total decimal    // arguments, then left-pads them with zeros to either    // 4 or 2 characters, as needed    '%04d-%02d-%02d %02d:%02d:%02d',    $ugly['tm_year'] + 1900,  // This will be "111", so we need to add 1900.    $ugly['tm_mon'] + 1,      // This will be the month minus one, so we add one.    $ugly['tm_mday'],     $ugly['tm_hour'],     $ugly['tm_min'],     $ugly['tm_sec']);echo $ymd;$new_schedule = new DateTime($ymd);echo $new_schedule->format('Y-m-d H:i:s');

If it works, you should see the same, correct date and time printed twice.


I think it is much cleaner to extend the DateTime class and implement createFromFormat() yourself like this:-

class MyDateTime extends DateTime{    public static function createFromFormat($format, $time, $timezone = null)    {        if(!$timezone) $timezone = new DateTimeZone(date_default_timezone_get());        $version = explode('.', phpversion());        if(((int)$version[0] >= 5 && (int)$version[1] >= 2 && (int)$version[2] > 17)){            return parent::createFromFormat($format, $time, $timezone);        }        return new DateTime(date($format, strtotime($time)), $timezone);    }}$dateTime = MyDateTime::createFromFormat('Y-m-d', '2013-6-13');var_dump($dateTime);var_dump($dateTime->format('Y-m-d'));

This will work in all versions of PHP >= 5.2.0.

See here for a demo http://3v4l.org/djucq