Carbon::now() is not using UTC
Asked Answered
N

6

20

I am from Philippines. If ever I will use Carbon::now(), it catches my machine time and not the UTC time.

My config/app.php is:

    'timezone' => 'UTC',

This is my code:

$log->dateRequest = Carbon::now();

If ever I will post a request at 9:00pm (Philippine time). It catches 21:00:00 , instead of 13:00:00 (from UTC).

Names answered 24/11, 2016 at 15:42 Comment(0)
A
33

In one of you questions you mentioned that you need to use multiple timezones in your app. So, you can add timezone dynamically:

Carbon::now('UTC')

Or use setTimezone('UTC') method on existing date.

Addison answered 24/11, 2016 at 15:50 Comment(0)
G
8

As stated in Carbon docs instantiation, try this:

$log->dateRequest = Carbon::now('UTC');

Goebbels answered 24/11, 2016 at 15:49 Comment(1)
I tried that at 12:21 am (Philippine time), but I got this 2016-11-25 00:21:23. It should be at 16:21:00 if it's converted in UTC.Names
A
4

Carbon uses the default DateTime PHP object.

Gets the default timezone:

date_default_timezone_get();
// or
Carbon::now()->timezoneName;

Sets the default timezone

date_default_timezone_set('UTC');

var_dump(Carbon::now()->utc); // true;
Aldrin answered 24/11, 2016 at 16:4 Comment(0)
P
2

you can also try this:

\Carbon\Carbon::now()->timezone('Asia/Manila')
Perorate answered 6/4, 2021 at 7:35 Comment(0)
R
2

you can try this

$log->dateRequest = Carbon::now('GMT+8')
Running answered 26/5, 2021 at 1:46 Comment(0)
K
-1

try this

date_default_timezone_set('Asia/Manila');
$date = Carbon::createFromFormat('F j, Y g:i:a', date('F j, Y g:i:a'));

dd($date->format('F j, Y g:i:a')); //  November 27, 2020 11:53:pm
Kristoferkristoffer answered 27/11, 2020 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.