Date/Time in WordPress

The Date/Time component includes all operations of input, output, and storage of information about time and dates in WordPress.

The WP code for working with time and a bunch of related functions were written a long time ago, back to PHP 4 version. There were a lot of shortcomings and bugs in the work of such functions, many of which were fixed in the WordPress 5.3 version. It is very difficult to fix everything due to backward compatibility.

Over a year of work was done on the code related to working with time in WP, which was released in version 5.3. Let's consider how Date/Time works now and what was done in WordPress 5.3. In short, the following was done:

  1. Stable and correct operation of all existing code related to dates and times. Errors were fixed, modular tests were added, and built-in documentation for many functions was corrected.

  2. New Date API functions appeared with WP 5.3+ for convenience and compatibility with PHP.

  3. The code was improved thanks to the new capabilities of PHP 5.6. Just a reminder, this is now the minimum version for WP.

New API Functions

Unified Way of Getting Timezone

  • wp_timezone_string() - a unified way to get the site's timezone, regardless of settings (options timezone_string/gmt_offset).

    The function will return the string Region/Location, for example Asia/Tashkent or the string ±NN:NN, for example +02:00. Both options are valid for PHP versions.

  • wp_timezone() extracts the site's timezone as a DateTimeZone object.

New Date Localization

  • wp_date() - the main function is a complete overhaul of date localization in WordPress. It works with Unix timestamps and PHP time zone objects, for example DateTimeZone.

The function date_i18n() is now considered deprecated and works based on wp_date().

Interaction with PHP

Moving Away from WP Timestamps

The WP Date/Time API relied on the so-called "WordPress timestamp" - WP timestamps - this is a Unix timestamp plus the site's timezone offset. This caused many errors and lack of compatibility with previous PHP versions or any external systems. The documentation erroneously mentioned Unix timestamps, when in fact "WP timestamps" were used.

It is impossible to remove WP timestamps without breaking backward compatibility. But developers have made significant progress to:

  • reduce their usage in the core.
  • correct incorrect documentation.
  • propose a new API using real Unix timestamps.

Thus, since WP version 5.3:

Not recommended:

Recommended:

Below are the DATE_RFC3339 formats.

--

Source: https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/