· 1 min read
What time is it?
Resources and snippets for working with timezones and dates in PHP using the Carbon library.

A helpful collection of resources about working with timezones for developers.
Watch
Read
Programming time, dates, timezones, recurring events, leap seconds... everything is pretty terrible.
PHP Conversion Snippet
Carbon is a time and date package for PHP. I use this snippet with Carbon to convert a user-provided date to UTC.
php
function date2utc($timestamp, $format = 'Y-m-d H:i:s', $zone = 'America/New_York') {
$class = CarbonCarbon::class;
$timestamp = $timestamp instanceof $class ? $timestamp->format($format) : $timestamp;
$date = CarbonCarbon::createFromFormat($format, $timestamp, $zone);
$date->setTimezone('UTC');
return $date;
}PHP Timezone Bootstrap
When I'm not using a framework that sets the time zone and character encoding formats, I set them myself.
php
date_default_timezone_set('UTC');
mb_internal_encoding('UTF-8');