Skip to main content

Posts

Showing posts from April, 2019

Handling timezone conversion with PHP DateTime

Handling timezone conversion with PHP DateTime Create function function convert_to_server_datetime($date, $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC') {     try {         $dateTime = new DateTime ($date, new DateTimeZone($userTimeZone));         $dateTime->setTimezone(new DateTimeZone($serverTimeZone));         return $dateTime->format("Y-m-d H:i:s");     } catch (Exception $e) {         return '';     } } Example: $userDate = '2019-04-19 13:20:00'; echo  convert_to_server_datetime ($userDate); Other Method public function convert($clienttimezone = null, $servertimezone = null){ $clientz=timezone_open("$clienttimezone"); $serverdateTime=date_create("now",timezone_open("$servertimezone")); $offset1 = timezone_offset_get($clientz,$serverdateTime); $servertz=timezone_open("$servertimezone"); $clientdateTime=date_create("now",timezone_open