This question already has answers here:
Using time zones in a PHP web application
(2 answers)
Closed 7 years ago.
I have tried searching, but do not seem to be clearly getting an answer for my question, but it seems so simple it maybe my newbie skills are still too green to understand, so please go easy.
I have created a sports prediction comp on my website using a MySQL DB & PHP connection.
All works, but one issue I have is that my website has members from all over the world. How do I get the time of the game, and the cut off time for submitting predictions, to show in their own timezone? All matches in the DB are set to the local time for the match itself (which is GMT in this case, but isn't always the case).
Appreciate the assistance of you experts to help me improve my skills. Sorry if it's a common question, happy to be pointed towards a relevant link.
Thank you again.
RB
The most common method is to use a UTC timestamp, then when displaying the time, adjust it to the locale of the user. This requires that a local for each user has been provided. If it has not, note the UTC on the timestamp when shown to them.
The UTC method ensures that you can accurately determine the TRUE sequence of insert/update events, using the table data, vs having to examine txn logs.
You can let your users specify their location (time zone) within their preferences and your DB uses its own, then do the math
Related
This question already has answers here:
How can I get the user's local time instead of the server's time?
(6 answers)
Closed 8 years ago.
I'm trying to get the date time based on user's timezone.I have tried with
echo date('Y-m-d H:i:s');
It is displaying correct date but the time is incorrect.May be it is taking server's timezone by default.Can anyone suggest me the better way to do it.
PHP is a server side language, so basically yo dont have any function that do it. you have to make the user send his time or timezone to you. for example, you can do it with ajax like here, javascript is a client side language that runs on the user computer and gets his time, then sends it to the server.
another way is to get the location of the user by his IP, there are many libraries that do it, for example this one, and create the date from his location
you can set default timezone and get time also what you want. suppose you want riyadh timezone then you can get by this code. you can put other timezone also what you want .here is the code
date_default_timezone_set('Asia/Riyadh');
here is list of timezome
http://php.net/manual/en/timezones.php
This question already has answers here:
Should I use the datetime or timestamp data type in MySQL?
(40 answers)
Closed 9 years ago.
I have searched around and haven't found a specific answer to my question..
I am wondering the best practice for storing timestamp information.
Example:
user1 logs in in Florida (-0500) at 3:00pm EST
The admin logs into the system an hour later from California (-0800) at 4:00pm
I want the admin to see the log onscreen and see that user1 logged in 1 hour ago..
My brain is going nuts bc i feel that i am over complicating this..
I should be able to use UNIX time stamp and then adjust with the -0800 or -0300.
Do i store the users timezone offset in their profile information?
Do i store the -0300 with the log entry.
Any input would be greatly appreciated!
You are mixing up client timezone and server timezone.
As long as you do not care where your users sit, you have absolutely no problem as long as you do not change the server timezone. You can set a default timezone in MySQL and in PHP and this one will be used, independent of the timezone of the visitor.
If you want to respect the timezone of your visitors, it gets slightly more complicated. You need do transform a timestamp from one timezone into another. You can find information concerning that in the PHP manual if you want to achieve this with PHP. You can read this SO thread if you are interested in how MySQL stores dates internally (difference between TIMESTAMP and DATETIME type), which opens the possibility to use some of mysql's features, and which you should definitely take into account when you implement a global project.
Timezone can be configured dynamically in the PHP, so you don't need to store time offsets in your DB. Storing unix timestamp is a good solution.
You can store timestamp as INTEGER datatype.
Then, you can use JS on client side to get the timezone of user.
This question already has answers here:
User recognition without cookies or local storage
(12 answers)
Closed 9 years ago.
Using PHP and MySQL:
Considering that two computers can share an IP address if they somehow use the same server. What's the better method to identify each machine and allocate it one count within 24 hours. Meaning a single machine can only increment view counts once every 24 hours regardless of how many views within this 24 hours.
That depends of the goal that you want to achieve. If this is some sort of non-necessary information (i.e. 'good to have it, but if not - then ok'), then you can use cookies or sessions. I.e. you should set a cookie with 24 hours lifetime and at each visit you should check if it is set.
But: if this data is mandatory, if it is a way for you to restrict something or - more - build some security, then the answer will be: no way. Client can always send fake data to overcome any possible restriction. Cookie is just an optional HTTP-request parameter (and it can be deleted, yes). The only more or less solid parameter is IP-address, but, however, it can be changed with proxy-servers too.
Conclusion will be: you know nothing about your visitor. This is a rule, when you're dealing with web-application.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Is there an equivalent to cron in Windows?
How do I update my data in MySQL periodically?
Say I have table. One column is "Date". Another column is "Expiry_Status".
How do I update the table periodically(a few hours) such that, if the date in the "Date" column is after the current date, the "Expiry_Status" will be updated to "Yes" value.
I am using WAMP as the framework.
Edit: Thanks for the answers but a lot of you guys gave answers about changing the database architecture. The case I gave above is just a simple example. I am more interested on how databases are updated regularly. ie: the program used, the syntax, the paradigms....etc etc.
Thanks again.
You write a program to perform the update, and then run it using a schedule system.
On Windows (since you tagged the question WAMP), this is usually done with Task Scheduler.
Other operating systems usually use cron.
Changing a status column is redundant though. You can search for data where the expiry time is greater than NOW() instead of where the status is Yes. This sort of scheduled script is normally used when the old data is being deleted.
write a php script which checks times and updates appropriately then use windows task scheduler to run them at certain interval. (assuming windows os...)
What value does having this expiry_status even serve? If you have an index in that date/datetime field (which you would need to effectively update the expiry_status field anyway), you can just as easily query on...
WHERE `date` > NOW()
...in order to determine active records. That is in essence all you are going to be doing with this proposed update query anyway. Why maintain an extra field if you don't need to?
Well I know that this question may be asked many times but certainly I've few doubts In my mind, and by the way don't comment that what I've tried, I've tried many ways but am just asking what's the correct and easier way as there are many posts lingering out here with different suggestions for accomplishing these tasks, so I'll explode() my question into smaller questions...
So can you people just guide me where am going right or wrong as am sure many people are confused when it comes to date/time
1) Why/how to save time as UTC in MySQL using PHP?
Personally for this I post this using php $year/$month/$day and check the date using checkdate() in mysql date field. so is it ok or should I use timestamp and than explode the retrieved string on front end using php?
2) If I run server from India, should I record default time using date_default_timezone_set("Asia/Calcutta"); and than subtract and add time using php function or while posting only I should check users time zone selected from his user accound and accordingly set a condition kinda:
if(timezoneselected == +5.30) {
echo date_default_timezone_set("Asia/Calcutta");
} elseif(timezoneselected == +anytime) {
echo another country timezone
}
3) last question is how websites like gmail facebook etc manages time? I mean if they are saving datetime according to their server than how they show perfect posted time for each user, even gmail, if I send a mail to another user, my sent time and the person living in another country gets email at his printed local time I mean how we can do this, sorry am not able to explain you perfectly say this example,
facebook:
user from India posts, facebook shows posted 8 mins ago, 9mins ago, fine after sometime they show a real date, and that date is perfect according to the time I posted, however if a person from USA updates, say 8 mins ago 9mins ago on his profile but his original posted time is shown correctly to him, and even correctly to me?
sorry for this question but really this will help me understanding this date/time concept and will also be helpful to future users. Thank you!
Bottom line, you should store everything UTC
When you display times for a particular user, use a timezone of their choosing. Store the timezone of the user, like "Asia/Calcutta" and simply convert the time when displaying it using the date_default_timezone_set method.
I will attempt to answer your questions from the comment here.
You store everything UTC always. It is the baseline. When you display the times associated with anything you convert based on the user. If you want to display Posted 8 mins ago then you are taking the delta between the current UTC time and the UTC time associated with a post. If you send a message from user A (in India) to user B (in Los Angeles, USA) then you would store the message time in UTC. If user A is viewing it, the time would be converted to "Asia/Calcutta" and if user B is viewing it, the time would be converted to "America/Los_Angeles". Using UTC will make your life a lot easier. Trust me.
As described in MySQL Server Time Zone Support:
The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.
Therefore, if you use TIMESTAMP type columns, MySQL will handle timezone conversion for you automatically: just set the appropriate timezone for the session in its time_zone variable.