Skip to content

Instantly share code, notes, and snippets.

@gioiliop7
Created January 5, 2023 16:00
Show Gist options
  • Save gioiliop7/a7c2af6de6732b034caa66b919efce1e to your computer and use it in GitHub Desktop.
Save gioiliop7/a7c2af6de6732b034caa66b919efce1e to your computer and use it in GitHub Desktop.
[PHP] Get current date or time of specific timezone
// If our server has a timezone out of Greece as an example , we can use this function to get the time of that timezone
// Supported timezones https://www.php.net/manual/en/timezones.php
// Supported time & date formats https://www.php.net/manual/en/datetime.format.php
function GetCityTimeDate($format,$timezone)
{
$tz = $timezone;
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
return $dt->format($format);
}
//An example of Athens
$Athens = GetCityTimeDate("H:i:s",'Europe/Athens');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment