PHP Date() timestamp calculation error - php

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:

Related

Converting date to UTC in PHP

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.

php date() returns one hour ahead of original when given integer date

The php date() function is returning a strange result. For example:
date("d/m/Y H:i",$sr1["parking_start"]);
Here the $sr1["parking_start"] is the date in integer format retrieved from the database. It should return the result 2016/4/24 15:30, but it returns 2016/4/24 16:30 or 2016/4/24 14:30. I have tried my best to sort it out but in vain. If you people think this is a server time issue then let me tell you that it's not, because when I copy and paste the value of $sr1["parking_start"] and paste it to the date function of the other php file on the same server, then its works perfectly.
Can you help me? What can cause the date() function to return the wrong result?
Check your default timezone date_default_timezone_get (and make sure you set it correctly).
You might want to check what was the timezone of the script that saved the date and, if different, you will have to change between the zones

Timezone not changing after using date_default_timezone_set

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.

PHP date() shows tomorrow's date

I have an odd error.
If I call:
date("js M")
it shows tomorrow's date, I don't know what changed because yesterday that function was working just fine.
I haven't tested anything in production, as far as I can see, it only happens in localhost.
I already checked my clock and it is right, is there any other place where PHP fetches the date? What could have changed?
Thank you!
By Default the date() function uses a unix timestamp, which is always set to +0:00.
date_default_timezone_set('America/New_York');
If you set the default timezone, the unix timestamp used will apply the correct offset to your location and you should be getting the correct day for you no matter where you are.

PHP number of hours between current time zone and GMT time zone

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.

Categories