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

A Tale about Time - PHP.gent

A Tale about Time - PHP.gent

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

September 19, 2018
Tweet

More Decks by Hannes Van De Vreken

Other Decks in Technology

Transcript

  1. A Tale about Time
    @hannesvdvreken
    @phpgent

    View full-size slide

  2. Hi, my name is Hannes.

    View full-size slide

  3. A Tale about Time

    View full-size slide

  4. 2018-09-18T19:00:00+02:00

    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:
    September 18
    09/18/2018
    18/09/2018

    View full-size slide

  17. Storing time:
    1521261900 ($dt->timestamp)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  24. date()
    microtime()

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  27. Change in PHP 7.1: microseconds

    View full-size slide

  28. // <7.1
    $date = new DateTimeImmutable(‘now’);
    $date->format(‘u’); // 00000
    // 7.1+
    $date = new DateTimeImmutable(‘now’);
    $date->format(‘u’); // actual microsecond info

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  35. interface Clock
    {
    public function now(): DateTimeImmutable
    }
    class SystemClock implements Clock
    class TestClock implements Clock

    View full-size slide

  36. c ~ 299.000.000m/s

    View full-size slide

  37. Thanks for your time!
    @hannesvdvreken
    @phpgent

    View full-size slide

  38. Time for questions.
    @hannesvdvreken
    @phpgent

    View full-size slide