PHP Datetime::Createfromformat in 5.2

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();

DateTime::CreateFromFormat for PHP 5.2.14

You could 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

PHP createFromFormat for 5.2 version

You can use strptime()

PHP DateTime::createFromFormat adding 12mins when output format is g:ma

You aren't getting extra minutes. You are displaying the number for the month (12 for December). Change the format to g:ia

When you did !H:i you are setting the month, day, and year to the Unix epoch which would be Jan. 1, 1970. Hence you are seeing a 01 for January in those settings.

See the table here:
http://php.net/manual/en/function.date.php

date_create_from_format AND DateTime::createFromFormat functions breaking on Server

From your comment: Your PHP Version ist 5.2.17

but DateTime::createFromFormat is avaible since PHP 5.3

Look at the doc: http://php.net/manual/en/datetime.createfromformat.php

DateTime::createFromFormat issue on server , works in local machine

You state that your live server is running PHP 5.2. This is your problem.

PHP 5.2 does not include the DateTime::createFromFormat() function. This function was only introduced in PHP 5.3. See the manual page for confirmation of this.

Your code will not be able to run under PHP 5.2.

PHP 5.2 is very badly obsolete, so the only recommendation I can give you is to upgrade as soon as possible. Aside from all the missing functionality, you're also putting yourself at severe risk of a hack by running such an old an unsupported PHP version.

The current minimum supported version of PHP is 5.4, but if possible you should straight up to 5.5 or 5.6, especially since you're using 5.5 already as your development base.

PHP date_parse_from_format( ) alternative in PHP 5.2

<?php
function date_parse_from_format($format, $date) {
$dMask = array(
'H'=>'hour',
'i'=>'minute',
's'=>'second',
'y'=>'year',
'm'=>'month',
'd'=>'day'
);
$format = preg_split('//', $format, -1, PREG_SPLIT_NO_EMPTY);
$date = preg_split('//', $date, -1, PREG_SPLIT_NO_EMPTY);
foreach ($date as $k => $v) {
if ($dMask[$format[$k]]) $dt[$dMask[$format[$k]]] .= $v;
}
return $dt;
}
?>

Example 1:

<?php
print_r(date_parse_from_format('mmddyyyy','03232011');
?>

Output 1:

Array
(
[month] => 03
[day] => 23
[year] => 2011
)

Example 2:

 <?php
print_r(date_parse_from_format('yyyy.mm.dd HH:ii:ss','2011.03.23 12:03:00'));
?>

Output 2:

Array
(
[year] => 2011
[month] => 03
[day] => 23
[hour] => 12
[minute] => 03
[second] => 00
)

m-d-Y to d-m-Y PHP 5.2

$parts = explode('-', $dateString);
var_dump($parts[1] . '-' . $parts[0] . '-' . $parts[2]);


Related Topics



Leave a reply



Submit