Laravel convert timestamp into date with specific timezone

Viewed 1306

I am saving my timestamp in Laravel using the following.

Carbon::now()->timestamp;

How do you convert a timestamp into a specific time format with a specific timezone (using Carbon)?

$unix_time_stamp = "1572095520";
$date = Carbon::createFromTimestamp($unix_time_stamp);
$date->setTimezone('Pacific/Auckland')->format('Y-m-d H:i:s');

I am getting incorrect result on this one, should be Oct 27

2 Answers

Please try the following.

$unix_time_stamp = '1572095520';
$converted = Carbon::createFromTimestamp($unix_time_stamp,'Pacific/Auckland')
    ->toDateTimeString();

Go to config -> app.php and change 'timezone' => 'Pacific/Auckland',

Related