converting any date having timezone to UTC - php

I am working on converting any date having timezone to UTC.
i have offset i wants to convert UTC time from that particular time by adding/subs rating offset like +5300 can any one help me how write PHP code for the same.
I have tried following code :
$d = new DateTime('2010-01-31 20:30:00');
$d->modify('+5300 hours');
echo $d->format('Y-m-d H:i:s'), "\n";`

If the time stamp is 2010-01-31 20:30:00+5300, then you would want to do:
$d = new DateTime('2010-01-31 20:30:00');
$d->modify('-53 hours');
5300 stands for 53 hours, 0 minutes, not 5300 hours. Also you need to subtract that to get back to UTC, it is already ahead of UTC.

Related

Apply time offset to get current time

My server has 5 hours difference from my current location time. After I record an event I'd like to display current local time. My time is stored in db as DATETIME value (0000:00:00 00:00:00).
I can get offset time in seconds with PHP like this:
$offset = date('Z');
So, the time from db will be:
$time = '2016-03-31 01:40:13';
But if my time is offset by -18000 sec, i.e. -5 hrs, the display time should be
2016-03-30 20:40:13
So I've attempted:
echo date('Y:m:d H:i:s', mktime(strtotime($time + $offset)));
But it looks convoluted...
setlocale(LC_TIME,'YOUR_LOCALE'); for strftime formatting
AND
date_default_timezone_set('YOUR_TIMEZONE'); for timezone settings

How to get the UTC time in years and months

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

Shifting unix time stamp according to timezone

I know this question has been answered many many times. I came in to a solution to solve this and its goes like this. I store all time stamps for each post in UTC on the server. Now i need to display the time stamp for a given timezone. I do this:
$tz : requested timezone
$ts : timstamp on db
$newts : new timestamp
$datetime = date('m/d/Y g:i a', $ts);
$dt = new DateTime($datetime, new DateTimeZone('UTC'));
date_default_timezone_set(trim($tz));
$newts = $dt->format('U');
date_default_timezone_set('UTC');
However the resulting time stamp is 60~ seconds higher than what is should be.
What am i doing wrong?
You're close, all you need to do is create the original DateTime object based on the timestamp/server timezone then set the new timezone and print the result, like so:
$datetime = new DateTime('#'.$ts, new DateTimeZone('UTC'));
$datetime->setTimezone(new DateTimeZone($tz));
print $datetime->format('m/d/Y g:i a');
The unix timestamp will be the same regardless of the timezone (it is TZ agnostic). The offset occurs when displaying it for different time zones. This is you can test this by printing the unix timestamp for each different timezone (they will be the same).
The U format gives you the number of "seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)". That number does not depend on where on earth you are. So even if it might be 14:59 at your place while it's 10:24 at my place, the number of seconds since January 1 1970 00:00:00 GMT is the same at both our places. A "time stamp for a given timezone" does not make sense.

Datetime php conversions and usage with timezone

I am trying convert a utc time stored date to another time zone but i cant seem to get it right.
I have a time :
date1 = new DateTime('first day of the month');
date1.setTime(0,0,0); // Since using the first day of the month seems return the current time with different date
The default DateTime timezone is in UTC. The time i want to make reference is in 'Europe/Amsterdam' timezone. Any way i cant get the time in 'Europe/Amsterdam' timezone to be equivalent to the first day of the month time in UTC? (Uh, sorry my question was confusing.. let me just give an example to be clear). Im trying to query from a db.
If UTC date time is June 01, 2013. 00:00:00
I want to get get May 29, 2013 19:55:00.
I tried getting the difference between the two declared times with different timezones to get the time that i wanted but it seems it didnt work :(
My Edit/ Clarification:
If use this code:
$date1 = new DateTime('first day of the month');
$date1.setTime(0,0,0);
print_r($date1->format('Y-m-d H:i:s'));
I would get:
2013-06-01 00:00:00
Then if i use timezone:
$date1->setTimeZone(new DateTimeZone('Europe/Amsterdame'));
print_r($date1->format('Y-m-d H:i:s'));
I would get: (This is just a sample output):
2013-06-01 03:00:00
Because of time difference. Want i want to get is like the reverse: I want to get the datetime that when converted 'UTC' timezone i would get this: 06-01-2013 00:00:00 time. So my preffered output is : 2013-05-29 21:00:00 ...
You can do in an OOP way like so.
$date = new DateTime('2000-01-01 00:00:00', new DateTimeZone('Europe/Amsterdam'));
echo $date->format('Y-m-d H:i:s P') . "\n";
To set the default date in PHP, you can either set it in your ini file or in a PHP file like so:
date_default_timezone_set('Europe/Amsterdam');
Then to format the date, refer to http://www.php.net/manual/en/function.date.php for formatting.
In your case this would be:
date('j M Y' time());
Where j = day, M = month and Y = year.

One hour difference in date and time after adding Daylight Saving Time offset in php

HI All,
I'm trying to match today's date and time at Atalanta to a database value. I'm testing following code.
$date = new DateTime();
$newToday = $date->format('Y-m-d H:i:s');
$dateTimeArr = split(" ",$newToday);
$dateArr = split("-", $dateTimeArr[0]);
$timeArr = split(":",$dateTimeArr[1]);
$testTime = date("Y-m-d H:i",mktime($timeArr[0]+4, $timeArr[1], $timeArr[2], $dateArr[1], $dateArr[2], $dateArr[0])); // 4 is Daylight Saving Time offset
When I run the code, I found that there is 1 hour time difference if I check the time at http://www.timetemperature.com/tzga/atlanta.shtml
I'm adding the day light saving offset which 4 hours, but still the time I get is 1 hour more than the actual time. Why this difference is seen ? How to rectify this ?
EDIT
My server is at different time zone than Atalanta. I want to handle the time difference without knowing the timezones. For this, for each city we have added timezone offset in database.
Use the DateTime and DateTimeZone objects:
//EDT = Eastern Daylight Saving Time
$x = new DateTime(null, new DateTimeZone('EDT'));
echo $x->format("Y-m-d H:i")
I think that if you use America/New_York (which is in the same timezone as Atlanta) as the timezone, it will change accordingly between normal and daylight saving time.
$x = new DateTime(null, new DateTimeZone('America/New_York'));
echo $x->format("Y-m-d H:i")
Both versions output the same time for me.
Have you checked your server's (the computer that executes the actual PHP script) time? See if it has the right time

Categories