Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Tale about Time - PHPDay 2018

A Tale about Time - PHPDay 2018

Timezones, DST, human readable diffs, standardised formats, intervals, getting the last Tuesday of next month… Dealing with timestamps seems easy, but there’s a lot to it. It’s very easy to do it wrong. Learn how to do this properly and you’ll never be chasing weird time related bugs anymore!

Hannes Van De Vreken

May 12, 2018
Tweet

More Decks by Hannes Van De Vreken

Other Decks in Technology

Transcript

  1. A Tale about Time
    @hannesvdvreken
    @phpday

    View full-size slide

  2. Hi, my name is Hannes.

    View full-size slide

  3. This is a 50 minute talk
    in 25 minutes.

    View full-size slide

  4. “We'd like to start the happy hour
    30 minutes earlier than
    previously planned … and the
    best solution would be to move
    your talk”

    View full-size slide

  5. EDT = UTC-04:00
    CEST = UTC+02:00

    View full-size slide

  6. EDT = UTC-04:00
    CEST = UTC+02:00

    View full-size slide

  7. Helsinki
    Sunrise: 9:24 - 3:54
    Sunset: 15:13 - 22:50
    Quito
    Sunrise: 6:08 - 6:12
    Sunset: 18:16 - 18:19

    View full-size slide

  8. EST/EDT - March 12
    CET/CEST - March 24

    View full-size slide

  9. Weeks - 7 days
    Starts on Monday. Or Sunday?
    First week of the year?

    View full-size slide

  10. Months
    Lunar vs Tropical

    View full-size slide

  11. Years
    Lunisolar vs Tropical

    View full-size slide

  12. 2018AD
    1500BC

    View full-size slide

  13. Leap years
    Leap seconds

    View full-size slide

  14. Storing vs Expressing time

    View full-size slide

  15. Expressing time:
    6:00PM vs 18:00

    View full-size slide

  16. Expressing time:
    May 12
    12/05/2018

    View full-size slide

  17. Storing time:
    1521261900

    View full-size slide

  18. Storing time:
    1521261900
    >1970-01-01T00:00:00+00:00

    View full-size slide

  19. Storing time:
    1521261900
    >1970-01-01T00:00:00+00:00
    <2038-01-19T03:14:07+00:00 (32bit)

    View full-size slide

  20. Storing time:
    RFC3339:
    2018-03-17T10:15:00+05:30

    View full-size slide

  21. Storing time:
    RFC3339 extended:
    2018-03-17T10:15:00.000+05:30

    View full-size slide

  22. Storing time:
    Sortable (in UTC):
    2018-03-17T05:45:00.000+00:00

    View full-size slide

  23. Storing time:
    Sortable (in UTC):
    2018-03-17T05:45:00.000+00:00
    + user’s location for timezone & locale

    View full-size slide

  24. DateTimeInterface
    - DateTime (date, time, tz)
    - DateTimeImmutable

    View full-size slide

  25. $date = DateTimeImmutable::createFromMutable($mutable);

    View full-size slide

  26. Change in PHP 7.1: microseconds

    View full-size slide

  27. $date = $date->setTimezone('Europe/Rome');

    View full-size slide

  28. // Magic
    $date = new DateTimeImmutable('now');
    $date = $date->modify('first day of next month');

    View full-size slide

  29. “now + 5 hours”
    “first day of next month”
    "first weekday”

    View full-size slide

  30. “2016-03-00” → “2016-02-29”
    “2018-06-31” → “2018-07-01”

    View full-size slide

  31. “69-12-31” → “2069-12-31”
    “70-01-01” → “1970-01-01”

    View full-size slide

  32. nesbot/carbon - Mutable
    cakephp/chronos - Immutable

    View full-size slide

  33. interface Clock
    {
    public function now(): DateTimeImmutable
    }

    View full-size slide

  34. c ~ 299.000.000m/s

    View full-size slide

  35. (new DateTimeImmutable(‘now’))
    ->setPlanet(new Planet(‘mars’));

    View full-size slide

  36. Thanks for your time!
    @hannesvdvreken
    @phpday

    View full-size slide

  37. Time for questions.
    @hannesvdvreken
    @phpday

    View full-size slide