Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Timezone for Carbon Date

I'm trying to set the timezone for a date in a Carbon object. It works fine locally but on my production box it keeps giving me Bad timezone error.

I've tried:

$date->setTimezone('7'); $date->setTimezone('+7'); $date->setTimezone('7:00'); $date->setTimezone('+7:00'); $date->setTimezone('UTC 7'); $date->setTimezone('UTC +7'); $date->setTimezone('UTC 7:00'); $date->setTimezone('UTC +7:00'); 

No idea why it's complaining on my production box. Can't find documentation either on what is the "proper" format to enter here. Can someone please help.

FYI: local is windows, and prod is Ubuntu box.

like image 318
Rob Avatar asked Feb 19 '15 06:02

Rob


People also ask

How do I change the timezone on my Carbon?

First, you set the timezone you initially have, so if you store your date in the database as UTC, then set the default timezone to UTC, then use ->setTimeZone('Valid/Timezone') to make the change from what you'v had to the new timezone.

How do I change the date format in Carbon?

Carbon::parse($client->db)->format('d/m/y'); is probably what you're wanting. (parse the yyyy-mm-dd date in the database as a date, and convert it to d/m/y format).

How do I get current time zone in laravel Carbon?

php $timezone = date_default_timezone_get(); echo "The current server timezone is: " . $timezone; echo "<br />". date('m/d/Y h:i:s a', time()); $mytime = Carbon\Carbon::now(); echo "<br />".

What is the format of timezone?

A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC. A time zone offset of "-hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes behind UTC.


2 Answers

You can change the timezone with this:

$timestamp = '2014-02-06 16:34:00'; $date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Europe/Stockholm'); $date->setTimezone('UTC'); 

this format working fine to my Local(Ubuntu) and prod(Redhat) project.

like image 126
Safoor Safdar Avatar answered Sep 21 '22 00:09

Safoor Safdar


I think you should refer to the official php timezone list. In your case you can use

$date->setTimezone('Asia/Phnom_Penh'); 

for UTC+7:00.

like image 38
Anton Egorov Avatar answered Sep 25 '22 00:09

Anton Egorov