How can i convert a datetime string like this: 2012-09-03 22:00
US Estern to UTC timezone?
You should look at the DateTime
object and its related functions in the documentation.
If your input date is already in a string format, DateTime::createFromFormat()
will help you create an epoch-type integer date the object can work with. After that, it's just getTimezone()
and setTimezone()
.
You could convert your timestamps from DB to epoch time with strtotime
and wrap them in a special CSS class :
<span class="timestamp"><?php echo strtotime('2012-09-03 22:00'); ?></span>
And then with a bit of a Javascript (jQuery example), dynamically transform the unix epoch into a locale datetime :
$('.timestamp').each(function(){
var seconds = $(this).html();
var d = new Date(1000 * parseInt(seconds));
var t = (d.getMonth()<9 ? '0':'') + parseInt(d.getMonth()+1) + '/';
t+= (d.getDate()<10 ? '0' : '') + parseInt(d.getDate()) + '/';
t+= parseInt((d.getYear()<1000 ? d.getYear()+1900 : d.getYear())) + ' ';
t+= (d.getHours()<10 ? '0' : '') + d.getHours() + ':' + (d.getMinutes()<10 ? '0' : '') + d.getMinutes();
$(this).html(t);
});
This will output dates in the format mm/dd/yyyy hh:ii
, relative to browser timezone.