0

I want to have a feature where the user can set that he wants some daily reports to be sent to him every day at H:mm AM/PM his time.

I must then determine what time it will be on the server when its the specified time for the user. E.g if the user put 5:00 PM as the time when he wants emails to be sent, and its currently 12:00 PM for him while on the server its currently 10:00 PM, I want to determine what time it will be on the server when its 5:00 PM for the user.

How can I achieve this using PHP / Javascript?

I was thinking of generating a MySQL TIME string (e.g hh:mm:ss) via javascript, of the current user's time, passing it to the server, which would use this string in a mysql query and do TimeDiff() on it? Or is there any better method?

Note: The client has specified that the user's clock time must be used, and not his current timezone.

Ali
  • 261,656
  • 265
  • 575
  • 769
  • Timezones would still be the way to go imo – orhanhenrik Aug 21 '12 at 19:48
  • See my edit, the client doesn't want timezones to be used, he wants it to calculated from the clock – Ali Aug 21 '12 at 19:50
  • 1
    @ClickUpvote: That doesn't make any sense, IMO. – InternetSeriousBusiness Aug 21 '12 at 19:51
  • I suppose he wants to account for the users who never change their timezone when they get their computer and just adjust the computer clock. So their timezone could be one thing but the time they expect to see it working by is their computer clock time. – Ali Aug 21 '12 at 19:52

2 Answers2

3

In your Ajax query on the end of your url I'd do something like:

var ts = Math.round((new Date()).getTime() / 1000);
var url= "http://mysite.com/?usertime="+ts;

Then on the server side you could use something like

$EstimatedTimeDifference = time()-$_GET['usertime'];

So now you've got the time difference in seconds between the server and the browser's time (roughly, ignoring the time it took for the script to generate the time()). From there, it should be fairly easy. Round it and divide it by 3600 to get the amount of hours difference etc.

Is that enough information to get you going?

  • Its good enough to get the number of hours' difference, but not 100% accurate. E.g what if the actual time difference is 12.5 hours, the server would only get 12 hours out of that. – Ali Aug 21 '12 at 19:55
  • @Izzey Oh. One thing, I've heard that time() works differently on windows versus linux, if the server is linux and the user is on windows, will that have any impact on this? – Ali Aug 21 '12 at 19:58
  • @ClickUpvote the operating system of the user doesn't matter, since the date is calculated through javascript, you would have to compare how javascript and PHP format times, but I'm assuming it's pretty much then same, try Bobby's script(s) – orhanhenrik Aug 21 '12 at 20:01
  • Nope, using it on the local windows server seems to work, but testing it on the linux server doesn't work, it always returns 0 as the hour difference even though I'm definitely in a different timezone than the server – Ali Aug 21 '12 at 20:13
  • @ClickUpvote To convert to hours etc with more accuracy if you don't like the std return: http://stackoverflow.com/questions/3172332/convert-seconds-to-hourminutesecond – Bobby Burton Aug 21 '12 at 20:28
  • It isn't working accurately to begin with, the javascript output for getting the time is different than php's time() if the user is on windows and server is linux – Ali Aug 21 '12 at 21:27
1

Just use UTC time and compare apples to apples:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/UTC

http://php.net/manual/en/function.gmdate.php

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
  • can't use user's timezone, must use his clock time – Ali Aug 21 '12 at 19:51
  • Huh? UTC *is* his clock time, just represented in a universal format. I don't think you understand what UTC is, take a look at the links I posted. – Madbreaks Aug 21 '12 at 19:53
  • How would I convert the times to UTC on the client side and then on the server? – Ali Aug 21 '12 at 20:15