PHP get current local time based on GMT offset - php

There is a function date_default_timezone_set() in php which accepts timezone string like "UK/London". But unfortunately I have the GMT offset of the user only.
Let's say I am on GMT +5 and I want to get current time based on GMT +5. I did a lot of search on google but did not help.
Since, I do not have the time zone string I am looking for something like
$current_date = date("Y M D H:S, "GMT +5");
Please help

You can only exclusively use GMT offsets for right now, i.e. it's completely useless if the user told you his GMT offset half a year ago and you want to know now what time it is at his location. It could basically be anything within a range of a few hours. Even if you got the offset a few seconds ago, it may already be different by the time you do the calculations if you happen to hit a DST change exactly.
Having said that, you'll want to add/subtract the offset hours from the formatted time, which is really the only thing you can do.
$offset = 5;
$hours = (gmdate('H') + $offset) % 24;
echo $hours . gmdate(':i:s Y m d');
That's just an example, you'll need to handle minute offsets as well.

date_default_timezone_set('GMT');
$curTime = strtotime("+5 hours");
$current_date = date("Y M D H:S, $curTime);
set GMT timezone first or get GMT offset。
get timestamp
timestamp to date

Related

How to get the difference between the current time zone and GMT

I know you can get the difference (in seconds) between the servers time zone and GMT time using:
$diff = date("Z");
Is there a way to get the same thing, but for a specific date. For example if DST ends next Sunday, but I need to set a time in GMT for next Tuesday. How can I check the timezone offset for next Tuesday?
Use DateTime()
$dt = new DateTime('2013-12-01'); // <-- any date after DST ends
echo $dt->format('Z');
You can use the second parameter for date for this.
date('Z', strtotime("2013-08-27"));

Relative Time Vs Absolute Time In PHP

Please check the following examples:
$date1 = strtotime("tomorrow 4:00 PM");
$date2 = strtotime("16:00:00");
$date3 = strtotime("10 hours");
$date4 = strtotime("+1 day");
echo date("Y m d H:i:s",$date1)."<br>";
echo date("Y m d H:i:s",$date2)."<br>";
echo date("Y m d H:i:s",$date3)."<br>";
echo date("Y m d H:i:s",$date4)."<br>";
It gives me the output as below:
2013 06 10 16:00:00
2013 06 09 16:00:00
2013 06 09 20:50:25
2013 06 10 10:50:25
I am considering first two example($date1 and $date2) as absolute data and the last two as relative date. Now, with only given the $date1/$date2/$date3/$date4 variables, is it possible to say whether it is relative time or an absolute time please?
I did get a solution on another thread: PHP datetime string differentiation
But that worked until I considered the 2nd example($date2 as an absolute value), where it doesn't work. Also, may suggested for regular expression checks, but that doesn't seem reliable either.
I was just wondering if php had some integrated way to tell this either from its functions or DateTime objects. I searched for, but didn't found anything.
Looking forward to listen for your suggestions/feedbacks/possible solutions. Thanks.
There is no direct way, AFAIK but there is a trick that you can use with the second parameter to the strtotime function.
function is_absolute_time($time_string) {
$time_shift = time() + 60; // 1 min from now
$time_normal = strtotime($time_string);
$time_shifted = strtotime($time_string, $time_shift);
return $time_normal == $time_shifted;
}
The rationale is simple: If the time is absolute, a 1 min difference won't change the calculation by strtotime and both $time_normal and $time_shifted will be same. For relative times, however, the difference will be one minute (the value in $time_shift variable).
There is a caveat with this code though. This function will return FALSE even for absolute times (but not absolute dates) less than 1 minute from midnight. You can minimize this by changing $time_shift to:
$time_shift = time() + 5; // 5 seconds from now.
This code will now work properly until 5 seconds from midnight. I think you can go safely to as low as 2. There is an edge case that 1 second in future might not work.
To fix this problem altogether, you can try a different approach:
function is_absolute_time($time_string) {
$epoch = 0; // Epoch
$time_shift = 60; // 1 min from epoch
$time_normal = strtotime($time_string, $epoch);
$time_shifted = strtotime($time_string, $time_shift);
return $time_normal == $time_shifted;
}
You can try this last solution directly. I am just building up the reason for the solution throughout this post.
Given only the value 2013 06 10 16:00:00 the answer is simple: it's absolute. Whether this absolute timestamp was created as "absolute" timestamp or based on relation to another date is impossible to tell. All you have is "2013 06 10 16:00:00", there's no "residual relativeness" or anything of that kind still in it.
Even this is relative to the supposed birth of Christ though, which is relative to the earth floating around in space since the Big Bang... *trollface*

strtotime('today') returning incorrect time?

I am trying to create a select list starting from the current date of the user. I want it so that it is set to midnight in unix timestamp format.
This is all I'm doing:
$today = strtotime('today');
echo $today;
This is my result:
1333144800
which is: Fri, 30 Mar 2012 22:00:00 GMT according to Epoch Converter (incorrect by a couple hours.)
If you want strtotime() to return a timestamp relative to UTC (00:00:00 UTC instead of e.g. 00:00:00 UTC+2, if your system is set to a timezone with an offset of 2 hours against UTC/GMT), you need to specify that:
$today = strtotime('today UTC');
GMT (+0) time
echo $today = strtotime('today GMT');
echo "<br>" . $today = date("d-m-Y H:i:s", $today);
We expect that your server runs at GMT - that is the best (for maneuvering with time displays later). If not, you MUST adjust php.ini set this "date.timezone = GMT".
When you get that done, you will see 00:00 with my codes.
Then, you must develop function (ie DisplayDate()) in your script to display dates of your website correctly if
youre not in GMT area
or/and if you want for your users to see times in their timezone with timezone selection for example.
DisplayDate() should include support for daylight changes also (0, or +1 hour / summer and winter time).
strtotime( $time )
is designed to return a unix timetamp, meaning, it will return the number of seconds since jan 1, 1970. http://www.php.net/manual/en/function.strtotime.php
To get around this, use something like:
$today = date("d/m/Y H:i:s", strtotime('today'));
echo $today;
You might have to specifically set the time as well as the day:
$today_midnight = strtotime('today UTC 00:00');
You should check the timezone configuration in your php.ini file. In my case (I live in El Salvador) I had to change it like this:
date.timezone = America/El_Salvador

Understanding date processing with strtotime

I'm trying to get my head round someone else's code which they've written for handling the dates of when news stories are published. The problem has come up because they are using this line -
$date = strtotime("midnight", strtotime($dateString));
to process a date selected using a jquery calendar widget. This works fine for future dates, but when you try to use a date which is in the previous calendar year, it uses the current year instead. I think this is due to "midnight" finding the closest instance of the selected day and month.
I could remove the "midnight", but I'm not sure what the repercussions of this would be - is there a reason that the midnight could be there?
EDIT: this is the full block of code which handles the date. The date contains the time, which allows the user to publish an item at a specific time.
$array['display_date'] = '24 October, 2011 17:30';
$string = $array['display_date'];
$dateString = substr($string, 0, -5);
$timeArray = explode(':', substr($string, -5));
$hours_in_secs = 60 * 60 * $timeArray[0];
$mins_in_secs = $timeArray[1];
$date = strtotime("midnight", strtotime($dateString));
$timestamp = $date + $hours_in_secs + $mins_in_secs;
//assign timestamp to validation array
$array['display_date'] = $timestamp;
echo $array['display_date']; // Output = 1351094430 (Oct 24 2012 17:00:30)
This really depends on what $dateString contains. Assuming your jQuery widget delivered the time portion as well, your colleague likely wanted to remove the time portion. Compare the following:
echo date(DATE_ATOM, strtotime('2010-10-01 17:32:00'));
// 2010-10-01T17:32:00+02:00
echo date(DATE_ATOM, strtotime("midnight", strtotime('2010-10-01 17:32:00')));
// 2010-10-01T00:00:00+02:00
If your widget doesnt return the time portion, I dont see any reason for setting the date to midnight, because it will be midnight automatically:
echo date(DATE_ATOM, strtotime('2010-10-01'));
// 2010-10-01T00:00:00+02:00
Note that all these are dates in the past and they will result in the given year in the past, not the current year like you say. If they do in your code, the cause must be somewhere else.
Will there be repercussions when you change the code? We cannot know. This is just one line of code and we have no idea of any context. Your unit-tests should tell you when something breaks when you change code.
EDIT after update
The codeblock you show makes no sense whatsoever. Ask the guy who wrote it what it is supposed to do. Not only will it falsely return the current year for past years, but it will also give incorrect results for the minutes, e.g.
24 March, 2010 17:30 will be 2012-03-24T17:00:30+01:00
I assume this was an attempt at turning 24 March, 2010 17:30 into a valid timestamp, which is in a format strtotime does not recognize. But the approach is broken. When you are on PHP5.3 use
$dt = DateTime::createFromFormat('d F, Y H:i', '24 March, 2010 17:30');
echo $dt->format(DATE_ATOM); // 2010-03-24T17:30:00+01:00
If you are not on 5.3 yet, go through https://stackoverflow.com/search?q=createFromFormat+php for alternate solutions. There is a couple in there.

php date offset

This is my issue, i have a time stamp coming from php, the servers time is 3 hours off from mine. I have offset the time by three hours but quickly realized that at 2 am the date part of the time stamp reads the day before date (day and month) and that does not change till 3 am. This is an issue because the date is important and i need it to be accurate. I have tried the timezone change but cant seem to get it to work. I live in ohio so thats the time i need and the timezone the server is in is three hours behind. So one of two things can help me, a timezone change that works or offsetting the day by 3h, not only the time. Here is my current code:
$timechange = mktime(date("g")+3, date("i"), 0, date("m"), date("d"), date("y"));
$date = date("D, d M Y g:i",$timechange);
$now = new DateTime('now', new DateTimeZone('America/Ohio')); // whatever your TZ's name happens to be
$now->setTimeZone('America/ServerTZ'); // reset to your server's TZ
$datestr = $now->format('D, d M Y g:i'); // get TZ's time as a nice string
By doing the setTimeZone, you affect the OUTPUT of the function - internally the timestamp is unchanged.
Instead of adjusting the times yourself, try just setting the timezone beforehand.
See http://php.net/manual/en/function.date-default-timezone-set.php

Categories