Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carbon::now() is not using UTC

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

like image 743
Vahn Marty Avatar asked Nov 24 '16 15:11

Vahn Marty


People also ask

What does carbon :: now () return?

Carbon::now returns the current date and time and Carbon:today returns the current date. This is a sample output.

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 UTC time now in 24 hour format?

UTC time in ISO-8601 is 09:05:16Z. Note that the Z letter without a space.

How can I get current date in UTC?

SimpleDateFormat f = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); f. setTimeZone(TimeZone. getTimeZone("UTC")); System. out.


2 Answers

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.

like image 164
Alexey Mezenin Avatar answered Sep 17 '22 13:09

Alexey Mezenin


As stated in Carbon docs instantiation, try this:

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

like image 31
Object Manipulator Avatar answered Sep 21 '22 13:09

Object Manipulator