Skip to main content

[local-time]

Syntax

[local-time from_timezone="America/New_York" to_timezone="America/Los_Angeles" format="F j, Y g:i a" time="now" browser="false" browser_format="default" show_on_same_tz="false"]

Fields

AttributeTypeDefaultDescription
from_timezonestringWordPress timezoneThe timezone to convert from. Must be a valid PHP timezone identifier. If invalid, falls back to the site default.
to_timezonestringWordPress timezoneThe timezone to convert to. Must be a valid PHP timezone identifier. If invalid, falls back to the site default.
formatstringF j, Y g:i aThe PHP date format string for displaying the converted time. See PHP DateTime format for options.
timestringnowThe time to convert. Accepts any value parseable by PHP's DateTime or strtotime (e.g., now, 2024-06-01 12:00).
browserstringfalseIf set to "true", displays the time in the user's local browser timezone using JavaScript. If "false", displays the server-rendered time.
browser_formatstringdefaultThe format string for browser-side rendering. Uses dateFormat.js masks. Only applies if browser="true".
show_on_same_tzstringfalseIf set to "true", the time will be shown even if the user's browser timezone matches the source timezone. If "false", hides the time in this case (browser mode only).

Note:

  • All attributes are optional.
  • If browser="true", the output will be replaced on the frontend with the user's local time using JavaScript.
  • For advanced formatting, see the Date Format JS library documentation.

Frontend JavaScript Date Formatting

Date formatting is done via the handle Date Format JS library by Steven Levithan.

Examples

var now = new Date();

now.format("m/dd/yy");
// Returns, e.g., 6/09/07

// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM

// You can use one of several named masks
now.format("isoDateTime");
// 2007-06-09T17:46:21

// ...Or add your own
dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
now.format("hammerTime");
// 17:46! Can't touch this!

// When using the standalone dateFormat function,
// you can also provide the date as a string
dateFormat("Jun 9 2007", "fullDate");
// Saturday, June 9, 2007

// Note that if you don't include the mask argument,
// dateFormat.masks.default is used
now.format();
// Sat Jun 09 2007 17:46:21

// And if you don't include the date argument,
// the current date and time is used
dateFormat();
// Sat Jun 09 2007 17:46:22

// You can also skip the date argument (as long as your mask doesn't
// contain any numbers), in which case the current date/time is used
dateFormat("longTime");
// 5:46:22 PM EST

// And finally, you can convert local time to UTC time. Either pass in
// true as an additional argument (no argument skipping allowed in this case):
dateFormat(now, "longTime", true);
now.format("longTime", true);
// Both lines return, e.g., 10:46:21 PM UTC

// ...Or add the prefix "UTC:" to your mask.
now.format("UTC:h:MM:ss TT Z");
// 10:46:21 PM UTC

Custom Formatting Additions

These are additions to these plugins that have been added by Yoko.

ZZ Timezone Formatting

The renders out a two letter representation of the time zone. For example, instead of EST this will render ET.

Hooks and Filters

The following hooks and filters allow you to override certain shortcode functionality.

yoko_local_time_to_time_formatted Filter

This filter allows you to filter the server-rendered output date string allowing for custom formats that may not be supported by PHP's Date formatter.

	...
$to_time_formatted = apply_filters(
'yoko_local_time_to_time_formatted',
$to_time->format( do_shortcode( $atts['format'] ) ),
$to_time,
$atts
);
...

Example Function

Here is an example from SIOP that replaces the timezone string with a 2 character one.

/**
* Filter Yoko Local Time Shortcode Output to Replace 3 Character Timezone with 2 Character Timezone.
*
* @param string $to_time_formatted The formatted time.
*
* @return string
*/
function yoko_local_time_to_time_formatted( $to_time_formatted ) {
$to_time_formatted = str_replace( 'EST', 'ET', $to_time_formatted );
$to_time_formatted = str_replace( 'EDT', 'ET', $to_time_formatted );
$to_time_formatted = str_replace( 'CST', 'CT', $to_time_formatted );
$to_time_formatted = str_replace( 'CDT', 'CT', $to_time_formatted );
$to_time_formatted = str_replace( 'MST', 'MT', $to_time_formatted );
$to_time_formatted = str_replace( 'MDT', 'MT', $to_time_formatted );
$to_time_formatted = str_replace( 'PST', 'PT', $to_time_formatted );
$to_time_formatted = str_replace( 'PDT', 'PT', $to_time_formatted );
return $to_time_formatted;
}
add_filter( 'yoko_local_time_to_time_formatted', 'yoko_local_time_to_time_formatted', 10 );