Skip to content

Instantly share code, notes, and snippets.

@Kudratullah
Created May 19, 2022 06:48
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 Kudratullah/4ccb8b7bcd2d7eb9f3169d4f2eabf284 to your computer and use it in GitHub Desktop.
Save Kudratullah/4ccb8b7bcd2d7eb9f3169d4f2eabf284 to your computer and use it in GitHub Desktop.
<?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