I have this line of code:
<?= date('F dS, Y',$user_info['date_of_birth']) ?>
It gets the unix birtday date per user and converts it in a normal date.
The only problem is that it will show one day earlier.
For example, the unix date in the database is 11th may 1990.
In the view it will output, 10th may 1990.
Do you know how to fix this issue?
Thanks in advance
To explicitly answer your question, you can do the following:
<?=date('F dS, Y',($user_info['date_of_birth'] + 86400))?>
Where 86400 is the number of seconds in a day.
However, as others suggested, you can and should sync up the database and Php to UTC/GMT time.
For Php timezone, do the following at top of your scripts:
date_default_timezone_set('UTC');
For SQL, that depends on the database you're using (I don't believe you specified the database you're using).
Check into synchronizing your MySQL and PHP timezones. Check out this similar question/answer:
Set timezone in PHP and MySQL
It is probably time on you server is wrong. Is you have accesse to it shell you can check it by date command:
[centos#application ~]$ date
Tue Jul 5 04:17:00 UTC 2016
if not then you probably will have to do like this :
define('TIMEZONE_OFFSET', 86400);
<?=date('F dS, Y',($user_info['date_of_birth'] + TIMEZONE_OFFSET))?>
Again if you have access to mysql console check its timezone settings.
set-timezone-in-php-and-mysql
Also set manually php defaulte timezone:
date_default_timezone_set('UTC');
Related
I have my project based in India and my hosting server located somewhere that is at a difference of -5:30 HRs from Indian time.
So my time being stored in database is coming as based on there timings. How can I rectify this in PHP so that the time actually gets stored on India.
I tried using date_default_timezone_set('Asia/Kolkata');
but still the time gets stored with a difference of -30mins to -45mins. What could be possible code fort his so that the time sets exactly according to Indian timings. Any advice will be very helpful.
Step-1: Set timezone as asia/kolkata
Step-2: Now, first store the
date in local variable, example: $mydate = date('Y-m-d H:i:s'); and
just print the variable value to see, if it is the time as you want?
Step-3: If yes then store $mydate variable value in database.
You can set the timezone in the database.
Few more threads of the same topic are here:
MySQL Time Zones
Set MySQL database timezone to GMT
MySQL timezone change?
If you want to store the current date/time on your database and use the timezone you have. Try to use now() function.
Reference Link: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_now
As you said your server is running at -5.30 hours from India, get the dates like this:
date('d-m-Y H:i:s',strtotime('+330 minutes')); // this adds 5.30 hours to the current time.
I have an application that posts to an PHP script, I want the PHP script to basically grab the current time and date, and insert it into my SQL database.
I'm currently doing this by using '$time()' within PHP, and then passing that into my SQL DB. In order to retrieve the time and date back, I use 'gmdate("M d Y H:i:s", $time);'.
I have a few questions though:
When I test this, the time it saves is an hour behind, so how do I apply different time zones? (I'm currently London/England) - but that might not be the case for the user who use this application.
Where is PHP retrieving the time from? Is it local? From the server?
Within my SQL, what should I set the data type to be? Timestamp? Currently, I've set it to varchar - but with all these different date and time types, I'm not so sure? (Date, Datetime, Time, Timestamp).
This PHP is called every time the user opens the application, so I want to be able to see: 'ah, so I see this user opened the application up at 21:20 on Wednesday the 14th'.
I'm sorry if its a noob question, but theres so many time and date classes and functions for both PHP and SQL that my brain has over loaded!
For a start, PHP time gets it's time from the server it's running on.
But if you really want the time a record was inserted, you should do one of the following:
Create a field in the table of type datetime, and set the default to:
GETDATE()
This will set the time automatically without you having to do anything special.
If you need that at time of input, still use SQL:
update [tablename] set LastUpdate=GETDATE()
Doing it this way ensures that the time is exactly when the record was set.
The PHP Time() function returns the EPOCH time (Seconds since January 1 1970 00:00:00 GMT).
You can use date_default_timezone_set() along with strftime() or mktime() to convert this to the servers local time.
You could set this via your application for the user if they're in a different timezone.
I linked the PHP manual pages for each function listed above.
What about to create a DateTime Field on MySQL table Structure and use MySQL to grab and set the date with NOW()?. Let MySQL do most calculations, it will help you to optimize the response time of your PHP script.
Look into this example: http://www.w3schools.com/sql/func_now.asp
Following the example of that page, but for an UPDATE:
UPDATE orders set OrderDate=NOW() WHERE OrderId=9999
Setting Timezone will fix the issue. I guess.
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
I believe I'm going mad.
$expire = date('Y-m-d H:i:s', strtotime("+30 minutes") );
It works as expected when echoed, yet when I insert it into a custom table via the wordpress database class, into a datetime column it's showing current time minus 30 mins.
Am I mad?
Simply given the time frame you are working with, it makes me wonder if you are seeing a Standard/Daylight time conflict, assuming you observe daylight savings time where you are.
First, See if you experience the same issue with "+60 minutes" as a test. If it is then -60 in the database, then it may be a bug, however, if it is then the current time in the database, it may actually be a timezone issue.
Make sure all of your timezones are properly configured on your host OSes and in your database. If the database column is of the type "timestamp" then mysql converts it to UTC on storage, and back on retrieval, so a mis-configured timezone could cause a 1 hour offset there as well.
Hope This Helps!
All date and time functions are now dependent upon a correct timezone setting.
You can either do this in your script
date_default_timezone_set('America/Los_Angeles'); // for example
OR
Check your php.ini for this setting
date.timezone = UTC
And set it correctly for your specific timezone, here is a List of supported timezones which you will need either way
OK. So I have been trying to implement a timer. Now a very weird thing is happening and I can't understand why ?.
Basically I am trying to find the difference between the last access and the current time. I am storing the time of last access in the database. This value is according to the server time. But when I try the time() function of php it shows me values which are 5-6 hours behind the time that I have in the database.
For example: here is my code :
$t1= strtotime($played_row->timer); // Time from the database with CURRENT_TIMESTAMP
$t2= strtotime("now"); // Get the current time
It shows Year: 2012 Month: 01 Day: 21 - 05:28 pm for t2
and Year: 2012 Month: 01 Day: 21 - 10:28 pm for my timestamp values.
Can anyone tell my why is that ?
P.S: I am running the code on my computer itself.
At a guess I would say that your database and PHP are using two different timezone offsets.
Most likely this is a timezone issue: if you are in the Eastern timezone, you are 5 hours away from UTC right now. If one sytem is returning local time and another is returning UTC this is what you will see.
Try using date_default_timezone_set() to set the timezone in PHP that is used in your database.
date_default_timezone_set — Sets the default timezone used by all
date/time functions in a script
Alse see date_default_timezone_get() how to get ini-set timezone.
I currently use $curdate=date('Y-m-d H:i:s'); to enter a timestamp to my blog's MySQL.
The problem is that the timezone of my MySQL is 2 hours ahead. At least in the timesaving period (I don't know if it is going to be any different when the timesaving period is over).
How should I redefine $curdate so that it records correct time based on PST time?
You can set the timezone the PHP uses for the duration of the execution of your script with date_default_timezone_set().
If you need to do something in your own timezone later in the execution of you script, you can call it again to set it back.
Alternatively (better?), if you use the MySQL NOW() function in your query, the time entered into the database will be calculated by MySQL, according to it's own timezone.