A long while back, I wrote a short piece about how to make Linux servers set their local time to Europe -> London, which you can find here.
Today, I’m going to show you how to post a quick snippet into your WordPress to allow you to display what the local time is anywhere in the world. For us, we are on Europe/London time, and so, below, here is that very feature:
Local Time in London is [now_time]
The above tells you the current time in London… cool, huh?!
How to Make WordPress display the current time in a place, using a shortcode
This is a pretty cool little shortcode, which you might use to display times around the world… London, Paris, Milan, New York, Wherever-you-are…
The above code should be added to your theme’s functions.php file, or a helper plugin.
How to display it
Simply insert the relevant shortcode into your WordPress post or page, once you have saved the above function into your site’s active theme:
now_time
You’ll need to wrap this in square brackets “[” and “]” in your post or page.
What it does
This code creates a little function which sets the timezone to be the one we want (in this case, Europe/London), then uses PHP’s “date” function to ouput a formated “date” which is just the hours (one digit) minutes and AM or PM – in this case that is:
$i = date('g:i A');
You can find out more about PHP’s date function, here. But suffice it to say, different characters relate to different displays. Have a play with this!
This can be used to insert today’s date and time in a nice sentence too, for some dynamic content in your post. Like right now, in London, it is [now_time].
Worked for You?
Leave a comment below if this helped you to insert the time in your WordPress website using a shortcode, or even if this only helped you to know what time it is, right now, in London!! 🙂
Book Silicon Dales to work on your WordPress
Got a WordPress problem you just can’t fix? We can have our WordPress developers take a look. Click here.
If you’re looking at this page, and you think that the time shown is not correct, remember about caching! This site uses a variety of caching tools and techniques, which may well cache the output of this page, making it… not the time shown 🙂
The same would apply to your page, so you may well want to set a caching exclusion to the page or place this shortcode is being used… or simply don’t use your site to display the time, most users have a clock visible on their device. This is more of a practical PHP / WordPress shortcode example than a recommended technique.