Created
July 7, 2020 11:00
-
-
Save medigeek/7149f6e5e38725f2a308fb5c7603ea2c to your computer and use it in GitHub Desktop.
test time zone conversion using carbon
This file contains hidden or 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 | |
// date/time variables using CarbonImmutable: https://carbon.nesbot.com/docs/ | |
//set user time zone | |
$userTimeZone = 'Europe/London'; | |
//======================= UTC ============================================== | |
//now in UTC | |
//CarbonImmutable i.e. don't change the hour value when switching timezones | |
$nowUTC = CarbonImmutable::now('UTC'); | |
//$nowUTC = CarbonImmutable::parse('2020-05-31 23:12:58 UTC'); | |
//$nowUTC = CarbonImmutable::parse('2020-05-30 23:00:00 UTC'); | |
//$nowUTC = CarbonImmutable::parse('2020-05-31 01:00:00 UTC'); | |
//var_dump($nowUTC->isoFormat('YYYY-MM-DD HH:mm:ss ZZ')); | |
//hour now in UTC | |
$hourNowUTCFormatted = $nowUTC->isoFormat('HH'); // Carbon: HH == hour 00..23 | |
//minute now in UTC | |
$minuteNowUTCFormatted = $nowUTC->isoFormat('mm'); // Carbon: mm == minute 00..59 | |
//minute now in UTC | |
$secondNowUTCFormatted = $nowUTC->isoFormat('ss'); // Carbon: mm == minute 00..59 | |
//date now in UTC | |
$dateNowUTCFormatted = $nowUTC->isoFormat('YYYY-MM-DD'); | |
//date yesterday in UTC | |
$dateYesterdayUTCFormatted = $nowUTC->yesterday()->isoFormat('YYYY-MM-DD'); | |
//======================= userTimeZone ==================================== | |
//now timezone Europe/London | |
$nowUserTimeZone = $nowUTC->setTimezone($userTimeZone); | |
//var_dump($nowUserTimeZone->tzName); | |
//var_dump($nowUserTimeZone->isoFormat('YYYY-MM-DD HH:mm:ss ZZ')); | |
//echo "\n\n"; | |
$nowUserTimeZoneDateMonthDayFormatted = $nowUserTimeZone->isoFormat('MMM DD'); | |
$nowUserTimeZoneDateMonthDayYearFormatted = $nowUserTimeZone->isoFormat('MMM DD, YYYY'); | |
//hour now, timezone Europe/London | |
$hourNowUserTimeZoneFormatted = $nowUserTimeZone->isoFormat('HH'); | |
//+1 hour from now, timezone Europe/London | |
$hourNextUserTimeZoneFormatted = $nowUserTimeZone->add('1 hour')->isoFormat('HH'); | |
//======================= userTimeZone convert to UTC ======================= | |
//Convert from time zone to UTC | |
//get time an hour ago | |
$oneHourAgoUserTimeZone = $nowUserTimeZone->sub('1 hour'); | |
//Get current time in userTimeZone start of day: YYYY-MM-DD 00:00:00 | |
$startOfDayUserTimeZone = $oneHourAgoUserTimeZone->startOfDay(); | |
//Convert start of day, from userTimeZone to UTC: i.e. today Europe/London at 00:00:00 -> yesterday UTC 23:00:00 | |
$startOfDayUserTimeZoneToUTC = $startOfDayUserTimeZone->setTimezone('UTC')->isoFormat('YYYY-MM-DD HH:mm:ss'); | |
//fix for start of day, 00:00:00-00:59:59 europe/london time | |
if ($nowUserTimeZone->isoFormat('HH') == "00") { | |
$oneHourAgoEndOfHourUserTimeZoneToUTC = null; | |
} | |
else { | |
$oneHourAgoEndOfHourUserTimeZoneToUTC = $oneHourAgoUserTimeZone->endOfHour()->setTimezone('UTC')->isoFormat('YYYY-MM-DD HH:mm:ss'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment