Changing Timezone of Carbon Object Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Changing Timezone of Carbon Object without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am taking a string which I am parsing with Carbon and specifying the timezone at UTC. Then, I am switching the timezone to America/Denver and outputting a formatted string and the time value does not change. What am I doing wrong? I have searched all over and everything I have read shows me that this should work.

$date = Carbon::parse('2018-08-19 10:43:15', 'UTC');
Log::info($date->timezone->getName());
Log::info($date->format('n/j/y g:i A'));

$date->setTimezone('America/Denver');

Log::info($date->timezone->getName());
Log::info($date->format('n/j/y g:i A'));

My log returns:

[2018-08-19 11:23:17] local.INFO: UTC  
[2018-08-19 11:23:17] local.INFO: 8/19/18 10:43 AM  
[2018-08-19 11:23:17] local.INFO: America/Denver  
[2018-08-19 11:23:17] local.INFO: 8/19/18 10:43 AM  

Answer

There was no code error. This is a CentOS 7 server. When I changed my timezone I copied the UTC file to /etc/localtime overwriting the currently symlinked America/Denver file in /usr/shared/zoneinfo. The proper procedure is to delete the symlink of /etc/localtime and recreate it pointing to the correct timezone file. Because my America/Denver file had been overwritten by the UTC file then the server was returning America/Denver times as UTC.

My solution was the copy the America/Denver file from another server and overwrite my broken America/Denver file. This resolved my problem.

We are here to answer your question about Changing Timezone of Carbon Object - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji