Changing date and time between timezones

前端 未结 2 1454
[愿得一人]
[愿得一人] 2021-01-21 16:00

I have been trying to convert between timezones a given date and time.
Code speaks more than words, so here it is:

/**
 * Returns the date and time in the n         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-21 16:39

    If your stumbling on this, it can be done cleaner now with the DateTime Object.

    public function convertTZ($dateTimeString, $inputTZ, $outputTZ){
        $datetime = 
            \DateTime::createFromFormat('Y-m-d H:i:s', //input format - iso8601
            $dateTimeString, 
            new \DateTimeZone($inputTZ))
            ->setTimezone(new \DateTimeZone('$outputTZ'));
    
        //outputs a string, if you want the dateTime obj - remove ->format
        return $datetime->format('Y-m-d H:i:s'); //output format 
    
    }
    

提交回复
热议问题