Last active
May 8, 2023 07:38
-
-
Save anthonyeden/0cf8eb86f7e634b3d5ded4debc59cb84 to your computer and use it in GitHub Desktop.
Wordpress Date() and StrToTime() Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wp_date_localised($format, $timestamp = null) { | |
// This function behaves a bit like PHP's Date() function, but taking into account the Wordpress site's timezone | |
// CAUTION: It will throw an exception when it receives invalid input - please catch it accordingly | |
// From https://mediarealm.com.au/ | |
$tz_string = get_option('timezone_string'); | |
$tz_offset = get_option('gmt_offset', 0); | |
if (!empty($tz_string)) { | |
// If site timezone option string exists, use it | |
$timezone = $tz_string; | |
} elseif ($tz_offset == 0) { | |
// get UTC offset, if it isn’t set then return UTC | |
$timezone = 'UTC'; | |
} else { | |
$timezone = $tz_offset; | |
if(substr($tz_offset, 0, 1) != "-" && substr($tz_offset, 0, 1) != "+" && substr($tz_offset, 0, 1) != "U") { | |
$timezone = "+" . $tz_offset; | |
} | |
} | |
if($timestamp === null) { | |
$timestamp = time(); | |
} | |
$datetime = new DateTime(); | |
$datetime->setTimestamp($timestamp); | |
$datetime->setTimezone(new DateTimeZone($timezone)); | |
return $datetime->format($format); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wp_strtotime($str) { | |
// This function behaves a bit like PHP's StrToTime() function, but taking into account the Wordpress site's timezone | |
// CAUTION: It will throw an exception when it receives invalid input - please catch it accordingly | |
// From https://mediarealm.com.au/ | |
$tz_string = get_option('timezone_string'); | |
$tz_offset = get_option('gmt_offset', 0); | |
if (!empty($tz_string)) { | |
// If site timezone option string exists, use it | |
$timezone = $tz_string; | |
} elseif ($tz_offset == 0) { | |
// get UTC offset, if it isn’t set then return UTC | |
$timezone = 'UTC'; | |
} else { | |
$timezone = $tz_offset; | |
if(substr($tz_offset, 0, 1) != "-" && substr($tz_offset, 0, 1) != "+" && substr($tz_offset, 0, 1) != "U") { | |
$timezone = "+" . $tz_offset; | |
} | |
} | |
$datetime = new DateTime($str, new DateTimeZone($timezone)); | |
return $datetime->format('U'); | |
} | |
?> |
act like strtotime when invalid format what about
function wp_strtotime($str) {
try {
$datetime = new DateTime($str, wp_timezone());
} catch (Exception $e) {
return false;
}
return $datetime->format('U');
}
Or
function wp_strtotime($str) {
return strtotime( wp_date( 'Y-m-d H:i:s', strtotime( $str ) ) );
}
Hi @alewolf,
Yes, that's much easier and better these days. My functions were written before any of this landed in WP Core - years ago now! I might have to update this for the benefit of people coming across it these days.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey - WP has a helper to get a DateTimeZone instance from the current timezone settings in WP. Should make these functions considerably easier to read/maintain. And
wp_date()
now exists in WP Core :)