Skip to content

Instantly share code, notes, and snippets.

@petenelson
Created September 27, 2021 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petenelson/64f9d75752467ca80deb7b0b4752c6c0 to your computer and use it in GitHub Desktop.
Save petenelson/64f9d75752467ca80deb7b0b4752c6c0 to your computer and use it in GitHub Desktop.
WordPress: Timestamp to date/time
<?php
/**
* Formats a timestamp into a date and time based on WordPress settings.
*
* @param int $timestamp The timestamp.
* @param string $format Optional date time format.
* @return string
*/
function timestamp_to_date_time( $timestamp, $format = false ) {
if ( empty( $format ) ) {
$format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
}
$tzstring = get_option( 'timezone_string' );
$date_time = null;
if ( ! empty( $tzstring ) ) {
$date_time = new \DateTime( 'now', new \DateTimeZone( $tzstring ) );
} else {
$date_time = new \DateTime( 'now' );
}
$date_time->setTimestamp( $timestamp );
return $date_time->format( $format );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment