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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
`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.
Leave a Reply