Carbon now time wrong
Asked Answered
G

5

16

I just started using the Carbon extension (seems pretty sweet so far), but am confused on the Carbon::now() function. According to the docs, it seems as though this function should reflect the current time in the users current timezone, however, I seem to get a time that is an hour ahead of GMT.

i.e. Carbon::now() says 2015-01-01 17:26:46 when I am on PST and it is actually currently 2015-01-01 08:26:46.

Do I have to detect and put in a users local timezone for all instances?

What gives? (I very well may have a fundamental misunderstanding of how a website gets a users local time)

Gastrostomy answered 1/1, 2015 at 16:32 Comment(13)
now() actually should reflect the server timezone. It doesn't know anything about the end-user's timezone.Reconvert
Thats odd because my server stores as my local time.Gastrostomy
Did you edit the timezone option on config/app.php?Reconvert
What does time(); give you?Crosscountry
I certainly don't think so. That sounds like something I should do...Gastrostomy
Your example 2015-01-01 17:26:46 is in fact 9 hours ahead of 2015-01-01 08:26:46 was this a typo or was this something you missed?Crosscountry
I definitely noticed that. But it is 9 hours ahead of my time, not behind.Gastrostomy
Are you sure the server is located in the same timezone as you? If not, that'll be the reason why.Crosscountry
developing locally. I will look into the server stuff more... don't want to waste your time. I'll definitely let you know if I get it.Gastrostomy
I gave the wrong thing by mistake, what does echo "The time is " . date("h:i:sa"); give you?Crosscountry
'The time is 05:59:33pm'Gastrostomy
Hey, I just checked my phpinfo(), and the time zone was wrong. So I will change it in my php.ini file, and I am sure it will solve the issue. Thanks a bunch for the help!Gastrostomy
@Gastrostomy I provided an answer, please accept it & upvote it if you find it answers your question.Crosscountry
C
11

This appears to be because the timezone of your server is different to your own.

This could be caused by:

  • Server misconfiguration
  • Physical location of the server is in a different timezone
  • Policies of your provider could also cause this. If your provider decides they want to operate on the same timezone on every server they have throughout the world, this will cause issues.

The server's timezone appears to be CET (Central European Time) which is +1 GMT, as you described.

To fix this, you should change the timezone in your php.ini file (instructions are from the link):

  1. Open your php.ini file
  2. Add the following line of code to top of your php.ini file:

date.timezone = "US/Central"

Alternatively you should replace the US/Central timezone with the desired timezone as outlined here if you wish PHP to use another timezone.

Crosscountry answered 1/1, 2015 at 17:36 Comment(1)
thanks for the link of time zones I was searching for Asia/DubaiMehitable
G
18

Carbon is UTC based therefore simply doing Carbon::now() will output the time in UTC format

You have to specify the timezone for an accurate reflection of the dateTime in your city or area.

There are two ways to do this. You can either do:

Carbon::now('PST') OR Carbon::now('Continent/City') like (Carbon::now('America/Montreal') for example

Gimpel answered 20/11, 2015 at 2:25 Comment(1)
According to the docs, Carbon::now() is not UTC by default: $now = Carbon::now(); // will use timezone as set with date_default_timezone_set // PS: we recommend you to work with UTC as default timezone and only use // other timezones (such as the user timezone) on displaySandi
C
11

This appears to be because the timezone of your server is different to your own.

This could be caused by:

  • Server misconfiguration
  • Physical location of the server is in a different timezone
  • Policies of your provider could also cause this. If your provider decides they want to operate on the same timezone on every server they have throughout the world, this will cause issues.

The server's timezone appears to be CET (Central European Time) which is +1 GMT, as you described.

To fix this, you should change the timezone in your php.ini file (instructions are from the link):

  1. Open your php.ini file
  2. Add the following line of code to top of your php.ini file:

date.timezone = "US/Central"

Alternatively you should replace the US/Central timezone with the desired timezone as outlined here if you wish PHP to use another timezone.

Crosscountry answered 1/1, 2015 at 17:36 Comment(1)
thanks for the link of time zones I was searching for Asia/DubaiMehitable
P
9

Change your time zone in config/app.php

 'timezone' => 'YOUR TIME ZONE',
Precess answered 28/2, 2019 at 11:43 Comment(0)
P
2

For anyone else wondering why (sometimes) Carbon::now() results different than new DateTime() :

Running the following...

public function time()
{
    $date = Carbon::now();
    echo $date;
    echo "<BR>";

    return $date;
}

made me understand what's going on. It printed:

2020-09-09 18:03:15
"2020-09-09T13:33:15.824992Z"

The first one is correct and its timezone is the one set in config/app.timezone. Calling new DateTime() results the same. The second one is the result of laravel controller. It shows the date in UTC timezone. This is how laravel converts it (using Carbon's toJSON method). But the timezone of the date is correct and running:

$anotherDate = Carbon::parse('2020-09-09 18:30:00');
echo "diff_mins : " . $anotherDate->diff($date)->i;

shows:

diff_mins : 26

as expected.

Peppie answered 9/9, 2020 at 13:37 Comment(1)
Thanks god I saw this answerVociferate
L
1

There are two main ways to change the timezone.
Option 1
From your project root directory, open the config directory.
Edit the app.php file next and the timezone value from UTC to the desired timezone from the list of available timezones.
'timezone' => 'Asia/YOUR_LOCATION',
Option 2
Make a modification in the .env file to specify the timezone as follows:
APP_TIMEZONE='Asia/YOUR_LOCATION'
Then, add the following in the app.php file:
'timezone' => env('APP_TIMEZONE', 'UTC'),
Finally,
php artisan config:clear

Lapidify answered 19/3, 2023 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.