-
blarney27
I’m using the following to display the current date but for some reason today it is showing April 3, instead of April 2, any ideas why?
function current_date_shortcode() {
return date(‘F d, Y’);
}
add_shortcode(‘show_current_date’, ‘current_date_shortcode’); -
blarney27
I’m thinking it’s pulling in the UTC time rather than the local time. How do I fix that?
-
Alvind
Hi there,
The
date()
function pulls the current date and time based on the server’s time settings. Try setting your server’s time setting to your local time. -
blarney27
The hosting company said there’s no way of doing that and provided this link to figure it out, but I’m not a developer so not really sure what to do. Do I just copy and paste that code to Code Snipets???
https://www.php.net/manual/en/function.date-default-timezone-set.php
-
David
Hi there,
in Dashboard > Setting > General – what is the
Timezone
set to ? -
blarney27
Los Angeles
-
David
Ok, so its either the server as Alvind pointed out.
Generally it is defined in the php.ini – see here for a example reference on how to set that:https://www.inmotionhosting.com/support/website/set-timezone-in-php/
But the host should really be able to advise on how to do it on their servers, as they may be using some other app to override that.
Or its a plugin conflict, some plugins may rewrite the date time output.
-
blarney27
Do you know if this will work using code-snippets:
<?php
date_default_timezone_set(‘America/Los_Angeles’);$script_tz = date_default_timezone_get();
if (strcmp($script_tz, ini_get(‘date.timezone’))){
echo ‘Script timezone differs from ini-set timezone.’;
} else {
echo ‘Script timezone and ini-set timezone match.’;
}
?> -
No, the code just compares if your timezone setting matches LA.
If there’s a mismatch, it will output “Script timezone differs from ini-set timezone.”, otherwise, it will output “Script timezone and ini-set timezone match
It does not change anything.
-
blarney27
I tried, and it worked.
-
Alvind
Try using this snippet instead:
function current_date_shortcode() { $timezone = new DateTimeZone( 'America/Los_Angeles' ); return wp_date("F d, Y", null, $timezone ); } add_shortcode('show_current_date', 'current_date_shortcode');
Now it should display your local time.
-
blarney27
Will try, thanks.
-
Alvind
You’re welcome!
- You must be logged in to reply to this topic.