Jump to content

Time Calculation


andz

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.