I want to know how I could get the UTC time in year and months? I've scraped the UTC date and time from a website, but I have been to asked to show the full UTC format such as (2014-07-31 22:00:00), the problem I'm facing now is that our current time is 1 hour ahead than UTC time.
Here's the code I'm using:
$date_time = date("Y-m", $time_now) . "-". $wxInfo['DAY'] . " " . $wxInfo['HOUR'] . ":" . $wxInfo['MINUTE'] . ":00";
As you can see, for the Y-m(year and month) I use local time, but the DAY, HOUR, MINUTE values from the website which is in UTC. This $date_time is updated every half hour, if they update on "2014-07-31 23:00:00", our current time is "2014-08-01 00:00:00". so my code will output:
2014-08-31 23:00:00
How can I convert to the right UTC year and month?
Use DateTime()
with DateTimeZone()
// Set time to local time "now"
$datetime = new DateTime();
// Change timezone to UTC
$datetime->setTimeZone(new DateTimeZone('UTC'));
// Echo datetime in desired format
echo $datetime->format('Y-m');
The solution John Conde suggested works well for displaying dates and times in a specific timezone.
In your case, I think you may want to set the timezone to UTC on the whole script, so all your calls to date() and DateTime->format() automatically use UTC. That is really simple to do: at the beginning of your script, write:
date_default_timezone_set('UTC');
PHP will then use UTC as a timezone everywhere. See manual: http://php.net/date_default_timezone_set