1

I am attempting to access the Apple News "Create Article" API with PHP and I am getting the following error:

{"errors":[{"code":"INVALID_DATE_FORMAT"}]}

The documentation asks for: "The current date in ISO 8601 format"

So, I am getting the date and formatting like this:

$date = date(DateTime::ISO8601);

Which outputs this:

2016-04-26T07:04:53-0700

But I am still getting the error. Anyone know why?

Jari Keinänen
  • 1,361
  • 1
  • 21
  • 43
Tom Canfarotta
  • 743
  • 1
  • 5
  • 14

4 Answers4

3

Looking at the example responses on Apple's Api reference (https://developer.apple.com/library/ios/documentation/General/Conceptual/News_API_Ref/CreateArticle.html#//apple_ref/doc/uid/TP40015409-CH14-SW1), it looks like the date format is as follows:

2015-03-05T02:57:59Z

Note the 'Z' at the end which means Zulu (a.k.a UTC) so it might be worth converting your date and time to UTC as follows:

$date = (new DateTime)->setTimezone(new DateTimeZone('UTC'))->format(DateTime::ATOM);

If you definately required the 'Z' (Zulu) on the end, you can do the following:

$date = (new DateTime)->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d\TH:i:s\Z');

Side note: to load your own date / time (rather than using now) you can change the code to this:

$date = (new DateTime($yourDateTimeString))->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d\TH:i:s\Z');

e.g.

$date = (new DateTime('2016-01-01 00:00:00 +0400'))->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d\TH:i:s\Z');

Please beware! If you are creating a DateTime object, make sure it knows your original timezone. Otherwise converting to UTC will do nothing and Apple's Api will take in your time as UTC, not your local timezone. As a general rule of thumb, always store dates as UTC, then convert back to your user's local timezone when viewing.

Jamesking56
  • 3,683
  • 5
  • 30
  • 61
  • thanks! Do you have an example if I am getting the date like this? $date = date(DateTime::ISO8601); Should i be getting it another way? When i try to convert it to UTC it says i can't format a non-object. – Tom Canfarotta Apr 26 '16 at 14:46
1

use gmdate("Y-m-d\TH:i:s\Z"); or urlencode(substr(date("c"), 0, 19)."Z";

Denis Alimov
  • 2,861
  • 1
  • 18
  • 38
0

Quick answer:

date('c', $timestamp);

Check also: that you're providing dates as strings, e.g.:

(in your ANF file)

{
  ....
  "dateCreated": "2020-09-08T12:41:00+00:00",
  ....
}

Check the Metadata spec for more examples. Apple will handle (and respect) timezones properly.

haz
  • 1,549
  • 15
  • 20
0

It can be done by Standard use of date function

echo date('Y-m-d\TH:i:sO',time());

Details where last part of 'O' belongs to timezone for example '-0700'.

Rahul Singh
  • 918
  • 14
  • 31