2

I'm struggling with the Carbon::now(), I'm specifying the desired timezone in the now function and it is not giving the date time based on the given timezone. It always using the UTC timezone that's specified in the laravel.

Carbon::now() // giving me date time of UTC timezone
Carbon::now('Asia/Karachi') // still giving me the date time of UTC timezone
var_dump(Carbon::now('Asia/Karachi')) // I'm able to see the correct date time based on the timezone. 

So when I use the var_dump() I'm getting the correct date time based on the give timezone. Can anyone explain why it is nog returning the correct timezone from the Carbon::now('Asia/Karachi')? but dumping the instance showing the correct datetime.

I've also tried the php artisan cache:clear as well as php artisan config:clear

jps
  • 20,041
  • 15
  • 75
  • 79
Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80
  • actually it is not recommended to change timezone in the application. stick with default UTC and if you want to handle date in frontend, just convert it based on client's timezone. – Mohsen Nazari Jul 15 '21 at 22:42
  • what makes you think `Carbon::now('Asia/Karachi')` is giving the date/time in UTC? – tanerkay Jul 16 '21 at 00:36
  • @tanerkay because it is showing me the UTC date time. I googled, UTC Date time and the result was the same. – Asif Mushtaq Jul 16 '21 at 00:39
  • something's fishy. maybe show the output of the code? You're not mocking anything or using `setTestNow()`? – tanerkay Jul 16 '21 at 00:50
  • I'm using simple route to test. `Route::get('test', function(){ return Carbon::now('Asia/Karachi'); });` and the output of this is the following. `2021-07-16T00:58:47.154312Z` – Asif Mushtaq Jul 16 '21 at 00:59

2 Answers2

1

In config/app.php file change UTC to your desired timezone. It will change app timezone globally. Eg: 'timezone' => 'Asia/Dhaka',

Niloy Quazi
  • 107
  • 2
  • 12
0

It always using the UTC timezone

Where? If you're talking about a JSON output, it's expected, date is formatted with ISO-8601 string and a trailing Z meaning Zulu = GMT timezone. The browser or client reading this JSON will have no issue to reconvert it then to any local date time using the user device timezone.

But at anytime if you dump explicitly this instance to a string using any format, the specified timezone will be used:

Carbon::now('Asia/Karachi')->format('Y-m-d H:i:s.u p')

(here using p or e you will see explicitly the timezone in the string.

KyleK
  • 4,643
  • 17
  • 33
  • Let say, I'm returning the `return Carbon::now('Asia/Karachi');` how the user can convert it? Let say, I want to convert it in the laravel frontend, blade or in any controller? – Asif Mushtaq Jul 17 '21 at 23:01
  • To format a Carbon instance in a blade template, refer https://stackoverflow.com/questions/50190058/change-date-format-in-blade-template if you return a JSON response, then you'll get an ISO string that contains the timezone so you can simply use `new Date(json.date)` in JS or equivalent in other languages. – KyleK Jul 18 '21 at 12:49