Skip to content

Instantly share code, notes, and snippets.

@anthonyeden
Last active May 8, 2023 07:38
Show Gist options
  • Save anthonyeden/0cf8eb86f7e634b3d5ded4debc59cb84 to your computer and use it in GitHub Desktop.
Save anthonyeden/0cf8eb86f7e634b3d5ded4debc59cb84 to your computer and use it in GitHub Desktop.
Wordpress Date() and StrToTime() Functions
<?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);
}
?>
<?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');
}
?>
@jdamner
Copy link

jdamner commented Aug 5, 2021

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 :)

function wp_strtotime($str) {
    $datetime = new DateTime($str, wp_timezone());
    return $datetime->format('U');
}

@alanef
Copy link

alanef commented Jun 15, 2022

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');
    } 

@alewolf
Copy link

alewolf commented May 8, 2023

Or

function wp_strtotime($str) {
    return strtotime( wp_date( 'Y-m-d H:i:s', strtotime( $str ) ) );
}

@anthonyeden
Copy link
Author

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