WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Easier Expression of Time Constants

The time constants were added in WordPress 3.5.0 to allow for easier expression of time intervals. Their use is encouraged with time-related functions where applicable. Take the following code for example:

set_transient( 'plugin_slugs', $plugin_slugs, 86400 );

Numbers are cool. But sometimes they stand in the way of what we have to say. From the context it's obvious that 86400 is a time period and with some calculation we can see it's 24 hours/1 day in seconds. Of course, it's not very readable, so usually in the code we see a clarifying comment:

// 1 day

This definitely helps. But with the new time constants, we can do better. We can make the whole thing read naturally in English:

set_transient( 'plugin_slugs', $plugin_slugs, DAY_IN_SECONDS );

This way we get all the information we need – we know the transient is set for one day, we can read this in plain English and we don't need to spare extra cycles to either make mathematical calculations or wait to the end of the line to see the actual period in the comment. The following constants were added, and are now used throughout core:

  • MINUTE_IN_SECONDS
  • HOUR_IN_SECONDS
  • DAY_IN_SECONDS
  • WEEK_IN_SECONDS
  • YEAR_IN_SECONDS

They are defined in wp-includes/default-constants.php, inside of wp_initial_constants().

Related

Ticked 20987

This article is marked as in need of editing. You can help Codex by editing it.