The get_gmt_from_date() WordPress PHP function converts a given date in the site’s timezone to UTC.
Usage
get_gmt_from_date($date_string, $format = 'Y-m-d H:i:s');
Parameters
$date_string
(string) – The date to be converted, in the timezone of the site.$format
(string) – Optional. The format string for the returned date. Default: ‘Y-m-d H:i:s’.
More information
See WordPress Developer Resources: get_gmt_from_date()
The inverse of this function is get_date_from_gmt(): https://developer.wordpress.org/reference/functions/get_date_from_gmt/
Examples
Convert a date to UTC
Converts a date in the site’s timezone to UTC using the default format.
// Set a date in the site's timezone. $date_string = '2023-05-08 14:30:00'; // Convert the date to UTC. $utc_date = get_gmt_from_date($date_string); // Output the UTC date. echo $utc_date; // Example output: '2023-05-08 12:30:00'
Convert a date to UTC with a custom format
Converts a date in the site’s timezone to UTC using a custom format.
// Set a date in the site's timezone. $date_string = '2023-05-08 14:30:00'; // Set a custom format. $format = 'Y-m-d H:i'; // Convert the date to UTC using the custom format. $utc_date = get_gmt_from_date($date_string, $format); // Output the UTC date. echo $utc_date; // Example output: '2023-05-08 12:30'
Convert a date to UTC and display in human-readable format
Converts a date in the site’s timezone to UTC and display it in a human-readable format.
// Set a date in the site's timezone. $date_string = '2023-05-08 14:30:00'; // Convert the date to UTC. $utc_date = get_gmt_from_date($date_string); // Format the UTC date in a human-readable format. $human_readable_date = date('F j, Y, g:i a', strtotime($utc_date)); // Output the human-readable UTC date. echo $human_readable_date; // Example output: 'May 8, 2023, 12:30 pm'
Display the current date and time in UTC
Displays the current date and time in UTC.
// Get the current date and time in the site's timezone. $date_string = current_time('mysql'); // Convert the date to UTC. $utc_date = get_gmt_from_date($date_string); // Output the current date and time in UTC. echo $utc_date; // Example output: '2023-05-08 12:30:00'