Skip to content

Repository files navigation

StellarWP Dates

TestsStatic Analysis

A collection of date utilities authored by the development team at StellarWP and provided free for the WordPress community.

This work is forked from the battle-tested date handling done at The Events Calendar!

Installation

It's recommended that you install Dates as a project dependency via Composer:

composer require stellarwp/dates

We actually recommend that this library gets included in your project using Strauss.

Luckily, adding Strauss to your composer.json is only slightly more complicated than adding a typical dependency, so checkout our strauss docs.

Documentation

How dates are built

The Dates::get() method accepts a number of parameters (a date, a timezone, a fallback date, and an immutable flag). However, you can get the current datetime with a simple call to Dates::get() like so:

useStellarWP\Dates\Dates;
$date = Dates::get();

Whether you are using that simple approach or something more complex, here's what is happening under the hood:

flowchart TD
is_immutable{"Immutable?"} -- yes --> yes_immutable["All dates returned will be Date_I18n_Immutable"]
is_immutable -- no --> no_immutable["All dates returned will be Date_I18n"]
yes_immutable --> is_timezone_set{"Is the timezone set?"}
no_immutable --> is_timezone_set
is_timezone_set -- yes --> create_timezone["Create a DateTimeZone object from $timezone"]
is_timezone_set -- no --> is_wp_timezone_set{"Is there a WP timezone string?"}
is_wp_timezone_set -- yes --> use_wp_timezone["$timezone = WP timezone string"]
is_wp_timezone_set -- no --> is_timezone_valid{"Is $timezone valid?"}
is_timezone_valid -- yes --> create_timezone
is_timezone_valid -- no --> use_utc["$timezone = UTC"]
use_wp_timezone --> create_timezone
use_utc --> create_timezone
create_timezone --> create_date["$date = date object using $datetime with $timezone"]
create_date --> is_error{Error when creating date object?}
is_error -- yes --> should_fallback{"Is a fallback date desired?"}
should_fallback -- no --> no_fallback((Return false))
should_fallback -- yes --> yes_fallaback["$date = 'now' date object with $timezone"]
is_error -- no --> final
yes_fallaback --> final((Return $date))
Loading

Constants

ConstantFormatDescription
DATEONLYFORMATF j, YThe date format used for date-only strings.
TIMEFORMATg:i AThe time format used for time-only strings.
HOURFORMATgThe hour format used for hour-only strings.
MINUTEFORMATiThe minute format used for minute-only strings.
MERIDIANFORMATAThe meridian format used for meridian-only strings.
DBDATEFORMATY-m-dThe date format used for date-only strings in the database.
DBDATETIMEFORMATY-m-d H:i:sThe date format used for date-time strings in the database.
DBTZDATETIMEFORMATY-m-d H:i:s OThe date format used for date-time strings in the database with timezone.
DBTIMEFORMATH:i:sThe date format used for time-only strings in the database.
DBYEARMONTHTIMEFORMATY-mThe date format used for year-month strings in the database.

About

A library with date utilities and classes.

Topics

Resources

Code of conduct

Stars

9 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages