1

Forgive me if this is discussed elsewhere on the forums but I can't seem to find it anywhere. How can we display the current time from the blog (or from New York, for instance) within a block of text? It would be even better if it continues changing each second after loading but not necessary.

This would be difference from displaying a post date, for instance.

  • 1
    You can install this plugin and use one of its shortcodes (you'd need to check which one fits your needs better). – montrealist Nov 8 '16 at 23:35
2

Take a look at current_time().

Returns the blog's current local time in the specified format. There are two named formats: 'mysql' for MySQL/MariaDB's timestamp data type format (i.e. YYYY-MM-DD HH:MM:SS), and 'timestamp' for the Unix timestamp format (i.e. epoch). Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d') since 3.9.0. The optional secondary parameter can be used to retrieve GMT time instead of the blog's local time.

The local time returned is based on the timezone set on the blog's General Settings page, which is UTC by default.

current_time( 'timestamp' ) should be used in lieu of time() to return the blog's local time. In WordPress, PHP's time() will always return UTC and is the same as calling current_time( 'timestamp', true ).

You can also modify the TimeZone of a date.

$d = date( 'c', time() );

echo $d; // 2016-06-12T16:35:39+00:00

$t = new DateTime($d);
$t->setTimezone(new DateTimeZone( 'America/Los_Angeles' ));

echo "\tLOS\t" . $t->format( 'g:i:s A' ); // 9:35:39 AM

$t->setTimezone(new DateTimeZone( 'America/New_York' ));

echo "\tNYC\t" . $t->format( 'g:i:s A' ); // 12:35:39 PM

echo ( $t->format('G') < 9) ? ' Before 9AM' : ' After 9AM';
2

If you are familiar with short codes and your functions.php, I have a solution for you. However, this will only display as static.

Add the following code to the bottom of your functions.php file.
<?php function displaydate(){ return date('g i: A'); } add_shortcode( 'date', 'displaydate' );

For me, right now in Arizona, this would output 5:07 PM

This code includes a function which checks the current time and then returns it when the shortcode is inserted in a post or page.

Now all you need to do to display the time is write your shortcode into one of your posts or pages. In this example, the shortcode is [date].

Take note of the characters after "return date", these are what control what is outputted.

You can adjust what is outputted in the shortcode by using different characters.

This link will give you a table of date format characters you can use. https://codex.wordpress.org/Formatting_Date_and_Time

1

WordPress deals with time in the UNIX format. You can display date and time as follows.

  1. If you are a beginner (developer) you can use <?php echo date('l jS F Y'); ?> to display at the place. It will change every time the page loads. To change it every second you will need to use an Ajax thing and some custom code.
  2. You can use a Local Time Clock plugin and use it as a widget and customize it as per your needs.

Not the answer you're looking for? Browse other questions tagged or ask your own question.