Carbon::now() is not using UTC

45,361

Solution 1

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.

Solution 2

As stated in Carbon docs instantiation, try this:

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

Solution 3

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;

Solution 4

you can try this

$log->dateRequest = Carbon::now('GMT+8'),

Share:
45,361
Vahn Marty
Author by

Vahn Marty

Updated on July 05, 2022

Comments

  • Vahn Marty
    Vahn Marty almost 2 years

    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).