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.
Related
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 currently using this to convert a timestamp to a user's defined timezone. My problem is that DateTimeZone() requires a timezone like Europe/Vienna or America/Chicago:
$date = new DateTime("#".$timestamp);
$date->setTimezone(new DateTimeZone("America/Chicago"));
I already looked into supported timezones on http://us3.php.net/manual/en/timezones.php but there are so many of them and I don't want users to browse the whole list.
Is there a simple way converting e.g. GMT+1:00, GMT-4:30 or GMT+5:45 to a correct value for DateTimeZone().
Or is it better to use an array list like I found here: Convert selected time and timezone to a set timezone
Is it better to use UTC or GMT in general for the user to pick?
Thanks!
You should probably obtain the user's real timezone (selected from the complete list). Using only a static offset from UTC you will not be able to follow the correct daylight savings time rules for the user's location.
PHP's DateTimeZone doesn't appear to accept POSIX timezone strings so it looks like you're stuck with predefined timezones.
Look in /usr/share/zoneinfo/Etc. There are a bunch of timezones predefined there for UTC plus and minus an integer. The only gotcha is that the sense of + and - is reversed from the normal convention. So just use, for example, "Etc/GMT-9" for a generic timezone with offset +0900.
This doesn't handle all of the possible timezones, like Nepal's weird +0545 but it looks like it's the best option that's easily accessible.
I don't think there's another way to pass a timezone. I think the time zones used in PHP have to be location-based to determine the DST settings along with the offset.
However, you can still present the options as GMT+1:00 etc to your users. And you can narrow down the list to only the much-used options (UTC, GMT, PST, etc).
I have an application that uses time() to record the time a topic was posted. I have done this for a long time and the only glitch i ever had with the method was that the time was always off by an hour (mainly a DST issue i never looked into).
I'm want to switch to the DateTime method, since I'm also switching to Twig, which uses that date format when setting a timezone.
But from what I can see, you can't use timestamps to parse the date. my question is, how do you input a date and parse it and what format are they looking for if it isn't time()?
By the sounds of it your server's default timezone is UTC. I suggest you try changing the default_timezone setting in php.ini to something that applies DST. This will mean that when you use date(), the resulting output will be correctly adjusted for DST.
you can't use timestamps to parse the date
There is a way, if your php version is >=5.3 :
$time = time();//or other time
$date = DateTime::createFromFormat('U',$time);
but as a matter of fact it is somewhat weird to use timestamp as DateTime does not rely on it. You can parse a date string directly with the datetime constructor if your date is english or "french" (i.e d/m/y). You can also create your custom format.
how do you input a date and parse it and what format are they looking for if it isn't time()
if it is a a standard format date :
$date = new DateTime($yourString);//would throw an exception if the format is wrong
if it is a custom format, like DD/MM/YYYY 14h32mins
$date = DateTime::createFromFormat('d/m/Y h\hi\m\i\n\s',$yourString);
if($date === false){//if format does not fit
exit('Your wrong !');
}
I have an application that uses time() to record the time a topic was posted.
Very bad practice. In MySQL you have DateTime format (and each relational data base system has his own equivalent) to store date times and there are fully compatible with the DateTime constructor in PHP so migrate to it (use FROM_TIMESTAMP to change into DateTime).
I have a list of dates stored in MySQL using PHP.
These were stored using the following code:
date_default_timezone_set('UTC');
$strDate = date("Y-m-d H:i:s",time());
I can only test this from my timezone, which is also UTC!
If a web visitor from eg Eastern Time USA views the page, will the date be converted to UTC correctly?
Presuming that I am storing the UTC dates correctly, what PHP function will display the UTC time, converted to the user's own timezone??
Firstly you're right to store the UTC; however remember that the date you have in PHP will be the server date - not the client date.
So to continue, read up on how to extract timezone based dates. Then consider how to extract the timezone from the browser - which you will need if you do the local timezone output in PHP rather than on the client.