mpalencia
mpalencia

Reputation: 5997

Laravel convert timestamp into date with specific timezone

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

Upvotes: 3

Views: 2407

Answers (2)

user633440
user633440

Reputation:

Please try the following.

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

Upvotes: 3

Aravinth E
Aravinth E

Reputation: 491

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

Upvotes: 1

Related Questions