2

What's the standard (or some kind of iso-conform thing) format to display date (year, month, day) and time (Hours minutes and seconds) together as an result of an API Request? Or should a timestamp be returned in this case?

1
  • Unix Timestamp; the number of seconds since 1970-01-01T00:00:00-00:00 (UTC). You should send the timestamp, but if you must send a formatted date, send the ISO8601, which looks like that.
    – Umbrella
    Feb 28, 2012 at 20:55

4 Answers 4

7

Use ISO8601 format. And use UTC timezone always. Its the standard by ISO and should be parsable by any library.

Example 2012-02-28T20:27:21+0000

In php use DateTimeZone and DateTime object combindly to get an ISO8601 date.

$dt = new DateTime("now", new DateTimeZone("UTC"));
echo $dt->format(DATE_ISO8601)`

See Date/Time in PHP manual to know more.

8
  • Don't forget the timezone to avoid confusions.
    – Michael-O
    Feb 28, 2012 at 21:05
  • @Michael-O Thanks. It should be UTC all the time. Z in ISO8601 means Zero zone. I added it. Feb 28, 2012 at 21:09
  • That PHP code adds the compressed offset rather than normalized Zulu time.
    – Michael-O
    Feb 28, 2012 at 21:11
  • 1
    The DATE_ISO8601 format pattern does not correspond to your example.
    – Michael-O
    Feb 29, 2012 at 9:11
  • 1
    You have changed the default timezone of the PHP env which might have implications the rest of the code.
    – Michael-O
    Mar 3, 2012 at 12:24
2

I think returning timestamp would be a better idea. It allows your API users to manipulate it easily. (My opinion)

1

Either do a integer unix timestamp or do full ISO8601.

0

RFC822 Format: 18 Feb 2012 14:27:18 -0000

DAY MONTH YEAR HOUR:MINUTES:SECONDS UTC-OFFSET

Using date():

date('d M y H:i:s O')

Twitter's API uses it, And some of its benefits are that it's readable by humans and easy to parse in PHP, JavaScript, And other languages. And you know which timezone the date is presented in.

1
  • 2
    RFC822 is not a very good format, neither language-agnostic nor string-sortable. It's a pre-ISO relic.
    – Michael-O
    Feb 28, 2012 at 21:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.