Knowledgebase

Handling Time, Dates and Scheduling Print

  • backenddevelopment, backend, woocommerce, troubleshooting, guide, howto, solution, zillionkinghost
  • 0

A persistent source of bugs.

WHAT TO STORE

Timestamps in universal time, always.

WHY

Local times are ambiguous, and change with daylight rules.

WHAT TO STORE ALONGSIDE, WHERE RELEVANT

The time zone the event related to.

WHY

Converting for display requires it, and it cannot be recovered afterwards.

WHAT TO BE CAREFUL WITH

The server's time zone differing between environments Daylight transitions, where an hour repeats or does not exist Assuming a day is always the same length

WHAT THAT AFFECTS

Scheduled jobs running twice or not at all, during transitions.

WHAT TO USE

A tested date library, not manual arithmetic.

WHAT NOT TO DO

Add a fixed number of hours to represent a day Compare dates as text Assume the client's clock is correct

WHAT TO VALIDATE

Dates received from clients, including implausible ones.

WHAT TO SET FOR SCHEDULED WORK

An explicit time zone.

WHAT TO PREVENT

Overlapping runs of a long-running scheduled job.

WHAT TO LOG

When scheduled work ran, and how long it took.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot