Quickest way to get timestamp of a time in a particular timezone in PHP

Playing with timezone conversions can be crazy, it still confuses me every time and I have to wait a minute to run some test code to figure out what’s it that I am trying to do here.

Anyway, the quickest way I have to get timestamp of a time in particular time zone is


<?php
$datetimezone_object = new DateTimeZone('Pacific/Nauru'); // new DateTimeZone('PST');
$date = DateTime::createFromFormat( 'Y-m-d H:i:s', '2014-04-14 05:00:00', $datetimezone_object );
echo $date->getTimestamp();

view raw

time.php

hosted with ❤ by GitHub

`DateTime` and `DateTimeZone` classes are what you need to know when dealing with timezones, ditch any other method of doing conversions in PHP. Seriously, there are no better ways of doing it.

And just a tip, if you already don’t know, timestamp from different timezones for a single point of time is same. It doesn’t depend on the timezone. Did you know? 🙂

Let me know in the comments if you have a question.


Comments

Leave a Reply

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