• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Home
  • Blog
  • Search

Kevin Dees

Full-Stack PHP Development

  • Twitter
  • Github
  • LinkedIn
You are here: Home / PHP / What time is it?

What time is it?

July 10, 2018 by Kevin Leave a Comment

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.

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.

date_default_timezone_set('UTC');
mb_internal_encoding('UTF-8');

Filed Under: PHP, Web

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Get Dare To Code

Get the latest on full-stack PHP development in your inbox with a monthly lesson from Kevin Dees.

Kevin Dees is a speaker, entrepreneur, blogger, and developer. He’s best known for his work in WordPress and podcasting for the retired SitePoint show. Kevin has developed sites for the brands Verizon, Denny’s, RIDGID, Michelin, and others. Currently, he is working on TypeRocket which powers over 10,000 professional websites and products internationally.

Copyright © 2021 · Kevin Dees · Log in