2

I have the DateTime format: 2016-04-06T15:17:42.97074Z that I obtained from v5 on the twitch api.

I need to turn this into the unix timestamp for comparison using PHP.

This is currently how I'm attempting the conversion into a DateTime object: $dateTime = DateTime::createFromFormat(DateTime::ISO8601, $json["created_at"]);, I have also tried using DateTime::W3C but to no avail.

$json["created_at"] definitely contains the time to convert as echo $json["created_at"] returns the time.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.