Get UTC from another timezone with Carbon
Asked Answered
D

1

81

How do I get the UTC date with Carbon if I use another timezone?

$timestamp = '2014-02-06 16:34:00';
Carbon::createFromFormat('Y-m-d H:i:s', $timestamp)->timezone('Europe/Stockholm');

I create with a Europe/Stockholm timezone. How do I get the UTC date from that (2014-02-06 15:34)?

Doubleheader answered 6/2, 2014 at 15:36 Comment(0)
D
161

You can change the timezone with this:

$timestamp = '2014-02-06 16:34:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Europe/Stockholm');
$date->setTimezone('UTC');
Doubleheader answered 6/2, 2014 at 16:37 Comment(5)
Or use the shorthand: $date->tz('UTC')Beverage
:] Or the property $date->tz = 'UTC'Holophrastic
@Beverage consider adding your comment as an answer.Lakeshialakey
I think that use more verbose, makes the code more readable. So I prefer the full word.Corin
this finally saved me to fix a backlog issue since last year :D finally!!Threat

© 2022 - 2024 — McMap. All rights reserved.