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.
Related
date('Y-m-d H:i:s','1345453380000'); should return 2012-08-20 09:03:00 but instead it returns 44605-09-21 02:00:00
I understand the time difference of one day may be due to me not specifically setting timezone in the conversion, but 38k years in the future is a bit off, where am I doing it wrong?
Is it the trailing zeros?
I appreciate any pointers... (BTW That timestamp is how certain apps deliver them, I did not craft it myself)
When I getdate() that same timestamp the same issue happens, so I don't think my code is wrong, rather something is problematic with the trailing 0's...
But even if I use the from human to timestamp converted, with epoch converter, I get wrong results.
ONLY if I remove ALL zeros it seems to return a proper date.
Why?
Note again, the timestamp is how it comes from an online "diary" App, and Epoch converter IS able to read it!
(https://www.epochconverter.com/)
What is happening here is that the timestamp is in milliseconds, but PHP expects seconds. Epoch converter works fine with both seconds and milliseconds. What you have to do is call date('Y-m-d H:i:s', ($timestamp/1000));
Look at this demo.
date('Y-m-d H:i:s',(1345453380000/1000));
Above code converts milliseconds to seconds.
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:
Running PHP 7.0.7, when I use simple date(), it outputs for example 9:00am instead of the real time that is 12:00.
The same code works just fine on my local(PHP 7.0.3), but not the server.
I have double checked the php.ini and server's time, both were correct (mysql's date function works fine on server).
TimzeZone is set on "Asia/Tehran".
Is there something that I am not aware of?
P.S. Tehran's time zone is "+4:30".
Try using gmdate instead of date().
Identical to the date() function except that the time returned is Greenwich Mean Time (GMT).
Hope this helps.
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
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.