PHP Datetime::Createfromformat Doesn't Parse Iso 8601 Date Time

PHP Unknown DateTime Format

createFromFormat rejects the fractional seconds when asked to parse ISO8601, but you can simply construct a DateTime using that string successfully, e.g.

$str='2016-04-06T15:17:42.97074Z';
//this will work...
$dt1=new \DateTime($str);
//but this won't...
$dt2=\DateTime::createFromFormat(\DateTime::ATOM, $str);

var_dump($dt1);
var_dump($dt2);

This will output the following in php 5.6.0 - 5.6.30, hhvm-3.15.4 - 3.17.1, 7.0.0 - 7.1.1

object(DateTime)#1 (3) {
["date"]=>
string(26) "2016-04-06 15:17:42.970740"
["timezone_type"]=>
int(2)
["timezone"]=>
string(1) "Z"
}
bool(false)

See also PHP DateTime::createFromFormat doesn't parse ISO 8601 date time

Possible causes for php string with ISO 8601 date conversion adds one more hour

the last Z on $data_start tells that the date is in UTC time.
I suppose your local timezone is UTC +1 ?

if you want to always work with UTC time, you can use something like : date_default_timezone_set ('UTC') to bypass your local timezone setting

Set default database DateFormat to ISO 8601 with timezones in Laravel

It’s really easy if you follow the documents.

PHP date function supports ISO 8601 since PHP5 and we can get it by passing the 'c' format character.

http://php.net/manual/en/function.date.php

Laravel model converts date attributes to Carbon objects. Carbon extends DateTime which has the format function that supports all of the date format characters.

You can easily create an accessor (or even a new custom attribute) to change a date attribute. Then use the Carbon format method to change it to ISO 8601 format.

So in a laravel model, we can do something like this:

public function getPublishedAt8601Attribute()
{
return $this->published_at->format('c');
}

and then we can access the attribute like this:

// Prints something like: 2016-10-13T21:48:00+03:00
echo $post->published_at_8601;

PHP DateTime createFromFormat will not die a natural death

createFromFormat is a static method, Try

$date = DateTime::createFromFormat(DATE_ISO8601, $eventdate);

instead.

it returns a DateTime object, which you're not capturing, so yes, you're "remembering" the previously set date, which happens to be "now", since you didn't pass in a date as a constructor argument.

PHP format date with milliseconds works but parse fails

Datetime will handle it fine, you won't need to create from format.

<?php
$format = 'Y-m-d\TH:i:s.v\Z';
$stringDateTime = (new \DateTime())->format($format);
var_dump(date_create($stringDateTime));

https://3v4l.org/phLE1

Result:

object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-03-13 18:07:30.005000"
["timezone_type"]=>
int(2)
["timezone"]=>
string(1) "Z"
}

This also works:

<?php
$format = 'Y-m-d\TH:i:s.u\Z';
$stringDateTime = (new \DateTime())->format($format);
var_dump(date_create_from_format($format, $stringDateTime));

$format = \DateTime::ISO8601;
$stringDateTime = (new \DateTime())->format($format);
var_dump(date_create_from_format($format, $stringDateTime));

https://3v4l.org/FcUqe

object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-03-13 18:15:10.011717"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-03-13 18:15:10.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+01:00"
}

Parse Date in PHP

Try This:

echo date('Y-m-d H:i:s', strtotime('2018-11-14T01:11:36.059Z'));

//Output
2018-11-13 20:11:36

Datetime ISO 8601

You input format is not valid. See supported input date and time formats.

Try this instead:

$tz = new DateTimeZone("Europe/Amsterdam");
$dateObject = DateTime::createFromFormat('!Y-m-d\TH', '2012-02-02T10', $tz);
echo $dateObject->format('j F Y H:i');

Demo.



Related Topics



Leave a reply



Submit