Home > Net >  How to convert any time zone into UTC using Laravel
How to convert any time zone into UTC using Laravel

Time:12-24

I want to convert the user's time, eg. 08:45 P.M, to UTC time zone. How can I do that?

if ($request->open_at)
{
  $time = Carbon::parse($request->open_at)->toUTCString();
  dd($time);
  $query->whereTime('open_at', '>=', $time);
}

CodePudding user response:

Like such, but unless you're always starting from the system timezone (configured in PHP), date must already have the correct timezone set for this to work, like others have mentioned.

$time = Carbon::parse($request->open_at);
$time->setTimezone('UTC');
...

Carbon extends the DateTime object including setTimezone

CodePudding user response:

Use this PHP approach:

$time = new DateTime("08:45 P.M");
$time ->setTimezone(new DateTimeZone("UTC"));
echo $time ->format("Y-m-d H:i:s e");

CodePudding user response:

Carbon has ->utc() method (equivalent to setTimezone('UTC')) and Laravel query build can take Carbon object without having to format it as a string:

$query->whereTime('open_at', '>=', Carbon::parse($request->open_at)->utc());

CodePudding user response:

Use the set timezone function to convert time

$time = Carbon::parse($request->open_at);
$time->setTimezone('UTC');
  • Related