Created
September 27, 2021 14:39
-
-
Save petenelson/64f9d75752467ca80deb7b0b4752c6c0 to your computer and use it in GitHub Desktop.
WordPress: Timestamp to date/time
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 | |
/** | |
* 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