andz Posted December 6, 2011 Share Posted December 6, 2011 Good day! I've been stumbled into a situation wherein the user have predefined time (this will be the expiration time) and the system will have to compare the user time based on server time. I've done the job in getting the time with timezone inclusion using javascript var userDate = new Date(); // output : Tue Dec 06 2011 16:18:23 GMT+0800 (PHT) I passed that javascript value using ajax to the server, the problem I encounter is the compare the current server date with the userDate. Any solution? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 6, 2011 Share Posted December 6, 2011 Do not store the users date/time based upon their timezone. Instead, store all date/times based upon the server and also save a value for the user's offset. Then when you pull date/times for display use PHP to modify the value based upon the user's offset. Quote Link to comment Share on other sites More sharing options...
andz Posted December 6, 2011 Author Share Posted December 6, 2011 Can you show me how? Do not store the users date/time based upon their timezone. Instead, store all date/times based upon the server and also save a value for the user's offset. Then when you pull date/times for display use PHP to modify the value based upon the user's offset. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 6, 2011 Share Posted December 6, 2011 Can you show me how? This forum is for people to get help with code they have written. If you want someone to write code for you, post your needs in the freelance forum and someone might be willing to do it for $$$. Have you even thought through what you would need to do and made an attempt? (I say this rhetorically because it is obvious you haven't). I'll provide some rough guidelines for you to build your solution. Then if you get stuck, post back with what you have and what isn't working. 1. Get the user timezone offset. You are already getting that value in the JavaScript code above. 2. Store the "current" datetime in the database using the database's timezone (Use NOW() or a field that automatically inserts the timestamp) or normalize everything to GMT see CONVERT_TZ(). Also, store the user's offset (i.e. +0800) 3. When you need to display a date time to the user, extract the datetime value from the database and convert to the user's timezone. You can do this in the query using CONVERT_TZ() or you can do it in the PHP code using strtotime() or another datetime conversion method. Quote Link to comment Share on other sites More sharing options...
andz Posted December 6, 2011 Author Share Posted December 6, 2011 Hi, thanks for your response and if my response offended you somehow. what i meant for can you show me is just a guideline and not a full working solution so that i know where to start. Can you show me how? This forum is for people to get help with code they have written. If you want someone to write code for you, post your needs in the freelance forum and someone might be willing to do it for $$$. Have you even thought through what you would need to do and made an attempt? (I say this rhetorically because it is obvious you haven't). I'll provide some rough guidelines for you to build your solution. Then if you get stuck, post back with what you have and what isn't working. 1. Get the user timezone offset. You are already getting that value in the JavaScript code above. 2. Store the "current" datetime in the database using the database's timezone (Use NOW() or a field that automatically inserts the timestamp) or normalize everything to GMT see CONVERT_TZ(). Also, store the user's offset (i.e. +0800) 3. When you need to display a date time to the user, extract the datetime value from the database and convert to the user's timezone. You can do this in the query using CONVERT_TZ() or you can do it in the PHP code using strtotime() or another datetime conversion method. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 6, 2011 Share Posted December 6, 2011 Hi, thanks for your response and if my response offended you somehow. what i meant for can you show me is just a guideline and not a full working solution so that i know where to start. No offense taken. I just gave you an outline of some of the things that need to be done. I don't have any sample code for this at my disposal, so the only way for me to provide anything would be to build it myself. There are many ways to accomplish this via PHP and MySQL and I'm sure there are some best practices. But, I think the most important decision is to normalize the times via PHP or MySQL. I would probably go with PHP since you can then re-purpose the code to work with another database if needed. PHP has a lot of built-in function for manipulating the timezone. I'd suggest reading through that section of the manual: http://www.php.net/manual/en/book.datetime.php Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted December 6, 2011 Share Posted December 6, 2011 Talking about it takes two minutes and is fun. Doing it takes an hour and is not fun. We're more than willing to talk to you about it, because it's fun and we like helping people. You'll have to pay us to write it for you, because we don't like doing things that aren't fun. The basics have already been given: Do all your date storage and calculations in a specific time zone. I use GMT because I work at companies that span countries. You can do it in local time for the server because you only have one. Either way, do it in a specific time zone and only display the user's timezone right at the end, when you're adjusting your output for the specific user. it's like this: Do you put commas in your numbers in the database, or do you only use number_format when you print the numbers? That's the theory you need to work on. Time Zones are a display-level feature, computers do time math in GMT. Quote Link to comment Share on other sites More sharing options...
Shadowing Posted December 19, 2011 Share Posted December 19, 2011 You saying that my apache server uses GMT always but when using strtotime its the server converting it? Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted December 19, 2011 Share Posted December 19, 2011 Nobody said that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.