Created
May 19, 2022 06:48
-
-
Save Kudratullah/4ccb8b7bcd2d7eb9f3169d4f2eabf284 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* | |
* Retrieves the date, in specified timezone and format. | |
* | |
* @param int|string $timestamp Unix timestamp. | |
* @param string $timezone GMT Offset or timezone string to output result in. | |
* E.G. +0400 or Asia/Dubai, +0630 or Asia/Dhaka etc. | |
* @param string $format Optional. PHP date format. | |
* | |
* @return string|false The date. False on invalid timestamp input. | |
*/ | |
function datetime_utc_offset( $timestamp, $timezone, $format = 'Y-m-d H:i:s' ) { | |
$timezone = new DateTimeZone( $timezone ); | |
//$datetime = date_create( '@' . $timestamp ); | |
try { | |
if ( is_numeric( $timestamp ) ) { | |
$timestamp = '@' . $timestamp; | |
} | |
$datetime = new Datetime( $timestamp ); | |
$datetime->setTimezone( $timezone ); | |
return $datetime->format( $format ); | |
} catch ( Exception $e ) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment