A Tale about Time - PHP Limburg

A Tale about Time - PHP Limburg

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!

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

December 05, 2019
Tweet

Transcript

  1. A Tale about Time @hannesvdvreken @phplimburg

  2. Hi, my name is Hannes.

  3. Genk

  4. Gent

  5. None
  6. A Tale about Time

  7. The earth is a globe

  8. Religion

  9. Dec 5, 2019 8:30pm CET 20:30, 5 december 2019 2019-12-05T20:30:00+02:00

  10. None
  11. None
  12. None
  13. 360° 60’ 60”

  14. None
  15. None
  16. None
  17. EDT = UTC-04:00 CEST = UTC+02:00

  18. None
  19. None
  20. None
  21. EDT = UTC-04:00 CEST = UTC+02:00

  22. # UTC+05:30

  23. UTC+05:45

  24. DST CET/CEST

  25. Helsinki Sunrise: 9:24 - 3:54 Sunset: 15:13 - 22:50 Quito

    Sunrise: 6:08 - 6:12 Sunset: 18:16 - 18:19
  26. None
  27. None
  28. 2019 EDT/EST- March 10 CET/CEST - March 31

  29. $

  30. None
  31. %

  32. None
  33. &

  34. '

  35. Calendars

  36. Weeks - 7 days Starts on Monday. Or Sunday? First

    week of the year?
  37. Months Lunar vs Tropical

  38. Years Lunisolar vs Tropical

  39. 2018AD 1500BC

  40. Leap years

  41. Leap years Leap seconds

  42. Storing vs Expressing time

  43. Expressing time: 6:00PM vs 18:00 (ISO)

  44. Expressing time: September 18 09/18/2018 18/09/2018 2019-12-05

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

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

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

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

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

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

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

    timezone & locale (europe/brussels)
  52. Time in PHP

  53. date() microtime()

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

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

  56. Change in PHP 7.1: microseconds

  57. // <7.1 $date = new DateTimeImmutable(‘now’); $date->format(‘u’); // 00000 //

    7.1+ $date = new DateTimeImmutable(‘now’); $date->format(‘u’); // actual microsecond info
  58. // Immutable $whenInRome = $date->setTimezone('Europe/Rome');

  59. // Magic $date = new DateTimeImmutable('now'); $date = $date->modify('first day

    of next month');
  60. “now + 5 hours” “first day of next month” "first

    weekday” …
  61. “2016-03-00” → “2016-02-29” “2018-06-31” → “2018-07-01” …

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

  63. nesbot/carbon - Mutable cakephp/chronos - Immutable $now->diffForHumas($date);

  64. interface Clock { public function now(): DateTimeImmutable } class SystemClock

    implements Clock class TestClock implements Clock
  65. The future

  66. E=mc²

  67. c ~ 299.000.000m/s

  68. None
  69. None
  70. None
  71. Warping up

  72. Wrapping up

  73. Thanks for your time! @hannesvdvreken @phplimburg

  74. Time for questions. @hannesvdvreken @phplimburg