Timezone not changing after using date_default_timezone_set - php

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.

Related

PHP Date() timestamp calculation error

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:

PHP TimeZone causes date() to show twice as fast as what it should

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.

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.

Converting MySQL datetime to RFC 2822 Using PHP date() Function

I'm setting up PHP's if-modified-since header on our webpages. I can detect if a php file has been saved and then properly set the header using PHP's filetime() and date() functions. However I also need to properly set the header when information in the database that gets displayed on the php page changes.
For the file being changed I use PHP's date() function like so:
date("r", $file_mod)
However when I use it on what I get from querying a MySQL datetime field, I always get the following response where the date, time, year, and month are wrong:
Wed, 31 Dec 1969 18:33:30 -0600
What is the proper way to convert from MySQL's datetime to RFC 2822, or at least how do I convert to a format to put into PHP's date() function to get the correct response?
SELECT DATE_FORMAT(exampledate,'%a, %d %b %Y %T') // given a MySQL datetime
date('r') // for PHP
You might consider using MySQL's date_format function in your query. That way the date will arrive in the correct format.
Check youre timezone settings
http://php.net/manual/en/function.date-default-timezone-set.php
functions like date() depend on these settings.
strtotime will do the job
If you don't mind, I'd like to see your implementation of "if-modified-since". I need to do that on one of my sites.

PHP date formatting issue

This PHP statement date('Y-m-d',1281394800) returns different values in different servers. One gives me 2010-08-09 and the other 2010-08-10. Could someone please help explain?
Try this and see if you still get different results:
date_default_timezone_set('UTC');
echo date('Y-m-d',1281394800);
If you get exactly the same results across servers, you can set the timezone to the one you want. For more information:
date_default_timezone_set
List of Supported Timezones
Are the servers in (or configured with) different timezones?
date()'s output is timezone-dependent.
Set the time zone:
date_default_timezone_set('UTC');
echo date('Y-m-d',1281394800);
date_default_timezone_set — Sets the
default timezone used by all date/time
functions in a script
http://php.net/manual/en/function.date-default-timezone-set.php
List of Supported Timezones

Categories