How to convert a date to a different time zone using Carbon in PHP?

To convert a date to a different time zone using Carbon in PHP, you can use the tz() method provided by Carbon. Here's an example:

use Carbon\Carbon; // Create a Carbon instance for the original date $date = Carbon::parse('2021-01-01 10:00:00', 'UTC'); // Convert the date to a different time zone $convertedDate = $date->tz('America/New_York'); // Print the converted date echo $convertedDate;

In the example above, we create a Carbon instance for the original date using the parse() method. The second parameter defines the original time zone, which is 'UTC' in this case.

To convert the date to a different time zone, we use the tz() method and pass the desired time zone as its parameter. In this example, we convert the date to the 'America/New_York' time zone.

Finally, we can print the converted date using echo $convertedDate.

Note that the tz() method returns a new Carbon instance representing the converted date. It doesn't modify the original Carbon instance.