Formatting dates to use timezone

David Carr

Tutorials PHP & MySQL

Working with dates can get complicated when you need a time to change depending on the time of year for GMT timezones. Thankfully DateTime makes it easier.

First create a new instance of DateTimeZone and pass in the timezone.

Next create a new instance of DateTime and pass in tthe date and timezone, then all that is left is to format the date. 

//set a date and time
$sendDate = '2019-05-16 17:30:00';

//set the timezone
$timezone = new \DateTimeZone('GMT');

//create a new instance of DateTime with the provided date and timezone
$dateTime = new \DateTime($sendDate, $timezone);

//format the date, in this case 2019-05-16T17:30:00
$sendDate = $dateTime->format('Y-m-d\TH:i:s');

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.