I'm looking for a way to find the number of hours of difference between the time zone of the PHP configuration and the GMT time.
For example if the PHP config time zone is set to America/New_York, find the number "-4"
if the in Brussels "+1" etc.
Thanks
Err, what if you try ;) ?
date('O');
You could use date_default_timezone_get() to get the timezone that has been set and determine based on this what the difference is from GMT. It will return the value from date_default_timezone_set() or the value set in date.timezone in the php.ini file.
Related
I have this date from my local timezone "2019-07-27T02:00:00"
I'm trying to save it to the database in a UTC timezone, so I can deal with it later and convert to to other timezone from UTC,
But when I do:
$date = new \DateTime("2019-07-27T02:00:00");
I got a:
DateTime #1564192800 {#519
date: 2019-07-27 02:00:00.0 UTC (+00:00)
}
Whatever I set here,
new \DateTime("2019-07-26T06:00:00")
I still get a UTC timezone, so this example
$date = new \DateTime("2019-07-26T06:00:00");
Will get me a result of:
DateTime #1564120800 {#519
date: 2019-07-26 06:00:00.0 UTC (+00:00)
}
It's like the timezone of the date (2019-07-26T06:00:00) is already in UTC? But it's not (?).
So, converting it to UTC with
$date->setTimezone(new \DateTimeZone('UTC'))
has no affect at all.
PHP probably thinks you're already in UTC, and accepts any DateTime object that doesn't have timezone explicitly listed as UTC as well.
You can check what PHP thinks is your timezone by looking in your php.ini file, or by using the command echodate_default_timezone_get(). Similarly, you can edit your timezone globally by editing your php.ini file or using date_default_timezone_set().
This is a list of timezones PHP can use.
As a side note: though it might seem like a good idea to keep your global timezone set to UTC and just use a DateTime/DateTimeZone to set local variables to the correct offset, don't do it. It'll affect the timestamps of your logs, and can lead to some very painful gotchas.
Seems to be a basic question but I can't find an answer other than "PHP handles it for you" and that isn't what I'm after.
Currently, if I echo date_default_timezone_get(); I get Europe/London
GREAT.
How can I tell if it is BST or GMT.
I need to display Europe/London BST or Europe/London GMT based on what it currently is at.
How can I reliably do this?
Simple enough - get the current time in UTC and compare it to the current time in Europe/London. If they differ, BST is in effect. If they're identical, it isn't.
Alternatively, use date()'s O, P, or Z parameters in essentially the same way - they should be +0000, +00:00, or 0 respectively if GMT is in effect. (I might work, too - I'm not sure if BST is technically considered "Daylight Savings Time"...)
I have this code :
echo date('Y-m-d',1445810400);
and it returns the date 2015-10-25 but it is wrong!
The real correct date is the 26th of December 2016, not anymore the 25th.
To solve this bug i have to add 3600 seconds (1h) to the timestamp value.
Is it a date() bug or am I doing something wrong?
check your php.ini what is the default timezone you have set for it. By default php.ini setting is UTC. Set to your timezone, and restart your web server. You should able to get the correct result. This one is by global.
Another way is in your php file, set the timezone by project. http://php.net/manual/en/function.ini-set.php
Here is your input data with this https://www.epochconverter.com/ screenshot:
How I can create a new date with different timezone? I have tested many ways, but doesn't work, example
$date = new DateTimeZone('UTC+5');
My goal is to create the $date, then convert it to "UTC-5" format.
UTC+5 is not a timezone, it's an offset. A location on earth, say, New York, America, has a local time which is x hours different from the UTC standard time. That's the offset, like UTC-5. However, this offset changes throughout the year based on daylight saving time. In winter the offset may be -5, but in summer -6. A timezone is something which describes this clearly, it's a ruleset for what offset the local time is to UTC and when that offset changes. A timezone is specific to a location and is called, for example, America/New_York.
Therefore, working with "UTC-5" offsets is pretty meaningless, and what you really want to do is use a timezone for the location you're working with.
I'm setting my default time zone for my page using:
date_default_timezone_set("America/Los_Angeles");
I have to set it there, because my server doesn't allow me to alter the php.ini file or .htaccess. The problem is, when I use this:
NOW()
to send the current time to my database, it still send it as the UTC timezone.
What I'm trying to do is display comments users display from a comment box on the page, and it's showing the time for each of the comments in the wrong timezone now.
date_default_timezone_set is a PHP function. It can only affect the behaviour of PHP.
NOW() is a database function, and changing your timezone in PHP has no effect on it. NOW() returns in the format YYYY-MM-DD HH:MM:SS.
time() is the equivalent PHP function. time() returns simply the number of seconds since the Unix Epoch. To get output in the same format as NOW(), use date("Y-m-d H-i-s");. This automatically uses time() underneath to get the current system time.
Read more:
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_now
http://php.net/time
http://php.net/date
From the manual
date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
Thus, use the date_default_timezone_set function only affects those functions. You should instead use time() instead.