Skip to content

Instantly share code, notes, and snippets.

@k-gun

k-gun/date_time_stuff.php

Last active Mar 7, 2016
Embed
What would you like to do?
Date time stuff.
<?php
function time_zone_offset_to_identifier(string $offset, int $offsetIndex = 0) {
static $timeZoneOffsets = [];
if (empty($timeZoneOffsets)) {
foreach(timezone_identifiers_list() as $timeZoneIdentifier) {
$dateTimeZone = new DateTimeZone($timeZoneIdentifier);
$dateTime = new DateTime('NOW', $dateTimeZone);
$timeZoneOffsets[$dateTimeZone->getOffset($dateTime)][] = $timeZoneIdentifier;
}
}
$dateTimeZone = new DateTimeZone($offset);
$dateTime = new DateTime('NOW', $dateTimeZone);
$dateTimeOffset = $dateTime->getOffset();
foreach ($timeZoneOffsets as $offset => $offsets) {
if ($offset == $dateTimeOffset) {
return isset($offsets[$offsetIndex])
? $offsets[$offsetIndex] : $offsets[0];
}
}
return null;
}
$id = time_zone_offset_to_identifier('+02:00');
print $id; // Africa/Blantyre
$id = time_zone_offset_to_identifier('+02:00', 100000000);
print $id; // Africa/Blantyre
$id = time_zone_offset_to_identifier('+02:00', 25);
print $id; // Europe/Istanbul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.