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

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"));

Related

strtotime() function returning incorrect date

I am getting the date of the next week even when I have set the timezone to UTC-9.
When UTC changes the day i.e 16-03-2017 00:00:00, and the UTC-9 timezone is still 15-03-2017 15:00:00, strtotime() function returns the date of the next week.
$last_time_instance = strtotime(''Wednesday' ' 16:00:00);
The variable $last_time_instance returns the time correctly, but forwards the date to the next week i.e. 22-03-17.
How can I resolve this?
You can append timezone UTC-9 to the string:
$last_time_instance=strtotime('Wednesday 16:00:00 UTC-9'); // 1490803200
$time_instance_formatted=date('d-m-Y H:i:s',$last_time_instance); // 29-03-2017 12:00:00

ISO date formats to wrong day

Short question but I can't get my finger on it. This piece of code:
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date));
returns Mon while
$date = '2015-12-08T00:00:00';
echo date('D', strtotime($date));
returns Tue. Why is that? The +01:00 is for the timezone, but that should not affect the day in my opinion.
First I've looked up that 08-12-2015 is in fact a Tuesday, so now we know the first one is incorrect.
PHP's date() is an Unix timestamp according to their own docs.
My belief is that adding the +1 as a timezone triggers the calculation to the +0 timezone (UTC) when asking for the day of the week and therefore returns 23:00 on monday as the current UTC time.
You can specify the timezone before executing the rest of the code: date_default_timezone_set('Europe/Amsterdam');
<?php
date_default_timezone_set('Europe/Amsterdam'); //this is an example of a +1 timezone, choose one from http://php.net/manual/en/timezones.php
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date) );
?>
strtotime will parse your date string using the supplied time zone or using the default timezone if unspecified. We can't see from the code you've posted what time zone your server is configured to, but once the date is parsed and converted to your time zone, the time may legitimately occur in the previous day, hence why you're seeing 'Mon'.
Either supply a time zone in the strtotime call via the now argument or set one globally with date_default_timezone_set.

PHP get current local time based on GMT offset

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

How to make PHP date() ignore local GMT setting?

I am storing the time of day as a number of seconds since midnight. I have a number that should be 8:00 am:
//3600 secs / hour * 8 = 28800
$time = 28800;
var_dump(date('h:i a', $time ));
My output is:
string(8) "01:00 am"
Based on my location, I am -7:00 GMT, so I can see where I would get 1:00 am, but how do I do format this time to show 8:00 am, essentially making it ignore the current GMT setting while formatting this time?
two ways.
first you may try gmdate() function which output the raw GMT time .
and the other way you can set timezone before you use date function.
as follow .
date_default_timezone_set('Asia/Shanghai');
echo date('H:i:m', time());
I figured this out. The solution is to use gmdate(). It will format a raw timestamp to GMT.

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

Categories