ExpressionEngine

2.11.9 User Guide

Retired Documentation

You are using the documentation for version 2.11.9. Go here for the latest version or check here for your available upgrades to the latest version.

Localize Class

Introduction

class Localize

ExpressionEngine’s Localize Class gives developers easy ways to work with dates.

ExpressionEngine stores all dates and times in UTC (Universal Coordinated Time), formerly known as GMT (Greenwich Mean Time). The benefit of doing so is that each member of an EE site can choose their own timezone and date localization settings. This permits entries and other information containing dates/times to appear in each member’s local time. ExpressionEngine uses the date/time and localization methods available in the Localize class to set and display dates and times throughout the application.

This class is initialized by the system automatically so there is no need to do it manually.

What time is it now?

property Localize::$now

To get a Unix timestamp of the current time, access the now property in the Localize Class. Using the now property rather than PHP’s time() accounts for the server_offset hidden configuration value so the most accurate time can be made available to ExpressionEngine and its add-ons.

ee()->localize->now;

Displaying time

Localize::format_date($format[, $timestamp = NULL[, $localize = TRUE]])

Generates a formatted date string based on a given date or the current date:

ee()->localize->format_date('%D, %F %d, %Y %g:%i:%s%a');
// Tue, April 09, 2013 3:07:11pm

ee()->localize->format_date('%r', 1345065960);
// Wed, 15 Aug 2012 17:26:00 -0400

ee()->localize->format_date('%r', 1345065960, FALSE);
// Wed, 15 Aug 2012 21:26:00 +0000

ee()->localize->format_date('%r', 499163160, 'America/Los_Angeles');
// Sat, 26 Oct 1985 01:26:00 -0700
Parameters:
  • $format (string) – Desired format of date using date formatting variables
  • $timestamp (int) – Unix timestamp to format, or current time if NULL
  • $localize (boolean) – Boolean of whether to use the current member’s timezone for localization (TRUE), or to use GMT (FALSE); or string of PHP timezone to use for the localization
Returns:

Formatted and optionally localized date

Return type:

String

Get time from a string

Localize::string_to_timestamp($human_string[, $localize = TRUE])

Similar to PHP’s strtotime(), the Localize class provides a way to take a pre-formatted date string, user input for example, and turn it into Unix timestamp for storage in a database. It’s important to use our this method instead of strtotime() so that the site’s or member’s localization is taken into account.

ee()->localize->string_to_timestamp('2015-10-21 06:30 PM');
// 1445466600 (Wed, 21 Oct 2015 22:30:00 GMT, localized from America/New_York)

ee()->localize->string_to_timestamp('2015-10-21 06:30 PM', FALSE);
// 1445452200 (Wed, 21 Oct 2015 18:30:00 GMT, treated as unlocalized)
Parameters:
  • $human_string (string) – Human-readable date
  • $localize (boolean) – Boolean of whether the passed date is pre-localized to the current member’s timezone (TRUE), or should be treated as GMT (FALSE); or string of PHP timezone the passed date should represent
Returns:

Unix timestamp representing the passed date string.

Return type:

Integer

Human-readable time

Localize::human_time([$timestamp = NULL[, $localize = TRUE[, $seconds = FALSE]]])

Returns a common human-readable date format conforming to ExpressionEngine’s default time formatting setting. This method is most commonly used to express dates in the control panel.

ee()->localize->human_time();
// 2013-02-15 03:35 PM
Parameters:
  • $timestamp (integer) – Unix timestamp
  • $localize (boolean) – Boolean of whether to use the current member’s timezone for localization (TRUE), or to use GMT (FALSE); or string of PHP timezone to use for the localization
  • $seconds (boolean) – Whether or not to include seconds, overrides include_seconds hidden config
Returns:

Human-readable date

Return type:

String