I need to convert a date and time (GMT) into a timestamp with php. The following code shows what I’m currently using:
<?php
$date="2012-06-29 10:50";
$timestamp = strtotime($date);
echo $timestamp;
?>
However, when I test the timestamp in an online convertor (http://www.epochconverter.com), the resulting date is 29th June 2012, 8:50 GMT, or 2 hours previous. Is it possible that the strtotime() function isn’t completely accurate and is just an estimate of the time? If so, are there better methods I could use for getting the exact time?
Thanks.
strtotimeassumes that you are converting a string in your server’s local time, so if the servers time zone is two hours out the result will be as wll.The comments in the manual suggest a couple of solutions, you can append UTC to your date:
Or you can change the default timezone for the script (this will apply to all other time functions!):
As a final alternative, you could try
date_create_from_formatwhich allows you to specify what exactly the format your string is: