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.
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 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');
I'm developing a website where I have to deal with different possible timezones from the users. This becomes a great problem since the website hosts time-delicate events like auctions.
All dates/times on the server are in UTC. Database stores everything in UTC timestamps. PHP default timezone is set to UTC too (date_default_timezone_set('UTC');).
Now, my problem is how I should interact with the users, whether I'm only showing a date or, more important, I'm reading a date/time from user input.
A concrete example:
An auction has a deadline, which I store in database as UTC.
When I view the auction on the website, a javascript timer uses a Date object to calculate the remaining time. It automatically converts the timezone to GMT+0100 (my local timezone). So if the deadline is '2013-08-08 10:46:08' (UTC), the javascript date object will return Aug 08 2013 11:26:15 GMT+0100 (GMT Standard Time).
If the current time is greater than 11:46:08 then the timer says that the remaining time is 00:00 (which is correct).
But if I try to insert a bid, the server accepts since the condition on the MySQL INSERT evaluates to true:
INSERT INTO Bids ... WHERE ... AND auction_deadline > NOW() ...
( because auction_deadline = '2013-08-08 10:46:08' and NOW() = '2013-08-08 10:26:50')
All this mumbo jumbo of timezone melts my brains. What am I missing here? I'm almost certain that storing all dates/times in UTC inside the database is the best. I just can't think crystal clear how do deal with it between the user and the database.
Your problem doesn't involve timezones at all, just the fact that clients can turn their clocks or have their clock skewed considerably. For that the fix is to poll the server every once in a while for an offset fix to use in calculations.
In fact, you don't even need date objects. There is a certain universal instant in time when the auction ends. Let's say it is 1375960662823. Right now, the universal instant in time is 1375960669199, so from that we see that the auction ends in 6 seconds (1375960662823 - 1375960669199 ~ 6000 ). It will end in 6 seconds regardless if I am in Morocco or Japan. Do you understand it yet?
To generate these numbers, on the client side you can call var now = Date.now() + skewFix where skewFix is the correction that needs to applied in case client has time skew or manually set their computer to wrong time.
In PHP, you can generate it with $now = time() * 1000;
This is rather a typical subject yet very complex for most to understand. First thing, you never mention the DAYLIGHT SAVING. yeah I am increasing your tension :).
Now let us see how we can do this. You did a good job by saving the Time in UTC. Now, I hope you have registered members and that each member has ability to set their preferred timezone, otherwise you will show Server' timezone based time to them.
When you through "start time" to user you must send them after converting UTC time to their time, similarly when you accept TIME from browser be it user action or javascript you need to convert that time to UTC considering the fact that user is that time zone that he select for his profile.
Hope that clear the idea on where you are going wrong? Please read through day light saving as that will play an important role too when you move ahead with other logic on same.
EDIT:
You can use javascript's Timezone offset, for auto submission and user input based on his settings.
Date in JavaScript uses local timezone. You should get UTC time for the user and send it to the server
new Date
Thu Aug 08 2013 17:00:14 GMT+0530 (India Standard Time)
(new Date("Thu Aug 08 2013 17:00:14")).toUTCString();
"Thu, 08 Aug 2013 11:30:14 GMT"
This will resolve the timezone issue between the server and client.
You said
( because auction_deadline = '2013-08-08 10:46:08' and NOW() = '2013-08-08 10:26:50')
In MySQL - NOW returns the current time in the server's local time zone (docs).
You probably want something like UTC_TIMESTAMP which returns the current time in UTC (docs).
Also - you probably shouldn't accept any input time from the client JavaScript at all. Only trust your own clock. When a bid is placed, use the time on your server in MySQL or in PHP. Don't accept it as input.
You can do the following
once page is loaded, send an ajax request to server with timezone offset of user. You can get timezone offset using the following code.
var curdate = new Date()
var offset = curdate.getTimezoneOffset()
offset is timezone offset in minute.
I think it will help.
everytime when you get the date from the clientside, you can use the getUTC to convert to UTC date ie:
var todayDate = new Date();
var todayDateInUTC = new Date(todayDate.getUTCFullYear(), todayDate.getUTCMonth(), todayDate.getUTCDate(), todayDate.getUTCHours(), todayDate.getUTCMinutes(), todayDate.getUTCSeconds());
so right before you insert the bid date to database, use the getUTC functions to convert it into UTC format.
I have a site that has list of stores in different countries ( different time zones ), and it should display when store is OPEN or CLOSED by working hours.
I have javascript that gets DEFAULT timezone GMT offset when daylight saving isn't set, put it inside mysql, and that part works correctly.
My timezone is GMT+1 ( and now it's daylight saving active, so it's +1 hour now )
I use php to change zone using this:
date_default_timezone_set('Etc/GMT+1');
echo date('h');
Time here: 10 PM
GMT time: 8 PM
And this code return: 7 PM
So it's like it instead increasing by 1, it decrease, and plus there is no daylight saving...
Can someone tell me what happen here?
Is it php bug or something wrong on server?
Thank you...
I'm currently chuckling because I recently had a similar problem. Unfortunately Etc/GMT timezones are deprecated. They break. My personal recommendation? Just use HH:MM or set it by the city (cities don't require daylight savings time adjustments!)
Perhaps date('I') might help in this situation.
Unless there is an option to define a look-up list [GMT offset] - [time zone name]. Then it will be possible to adjust time zone using DateTime\DateTimeZone classes.
I'm retrieving a unix timestamp from a DB and I want to check if this datetime has passed already.
I tried using an if statement to compare to time() but it always says the time has passed already.
What am I doing wrong?
EDIT: Just some more info..to determine am/pm I'm adding 12 to the hour if its PM before running it through mktime(). (Is this right?)
It's stored in the DB as int not as any datetime types.
Your PHP time could be affected by PHP's timezone. Use date_default_timezone_get() to find out what time zone you're in.
Make sure the timezones in the DB and PHP are the same, use NOW() function to fill the DB column with current timestamp (the column should be of datetime type), then you can get the timestamp using UNIX_TIMESTAMP() MySQL function which compares against PHP's time() just nice.
Alternatively, you can fill the DB column with something like
mysql_query("INSERT INTO your_table (your_date) VALUES (FROM_UNIXTIME(" . time() . "))")
That should work even with timezone discrepancies.
If you are using mktime to create a UNIX timestamp, PHP is using the timezone settings to interpret what you mean by the given parameters. It's possible that you should be using gmmktime. It depends on how the timestamps in the database are being created; I cannot say for sure without seeing more code and having a more detailed explanation.
I generally prefer to simply store all dates as DATETIME types in the UTC (GMT) timezone. It tends to be less confusing.
Just some more info..to determine am/pm I'm adding 12 to the hour if its PM before running it through mktime(). (Is this right?)
12 PM is hour 12.
1 PM is hour 13.
So you don't always add 12. (i.e., 12 Noon is the exception).