1

I have a page that shows some info and the date it was created (server time). I need some method to display the date in both server and user time.

Example:
Oct, 26 13:48:23 (server)
Oct, 26 14:48:23 (your time) -> info

What I have so far:

<?php
    $sql=mysql_query("SELECT * FROM table") or die("Come back later");
    while($row=mysql_fetch_array($sql)) {
        echo date('M, d H:i:s', strtotime ($row['date'])).' -> '.$row['info'];
    }
?>

Thank you.

EDIT: I think I did not explain this right. The date is stored in the DataBase (the date is a point in the future for a future event) and I want to show the date in user timezone.

For example, something's going to happen day 26 at 12:10:22 (server time) and I want to show a conversion to user time to make things easier for them.

EDIT 2 Example: With the diffence I want to detect user timezone.

  • Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/q0gwD). See the [red box](http://goo.gl/OWwr2)? Instead you should learn about [prepared statements](http://goo.gl/orrj0) and use either [PDO](http://goo.gl/TD3xh) or [MySQLi](http://in3.php.net/mysqli). If you can't decide which, [this article](http://goo.gl/YXyWL) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/b2ATO). Also see [Why shouldn't I use mysql functions in PHP?](http://goo.gl/J5jAo) – NullPoiиteя Oct 26 '12 at 13:49
  • is the server date stored in the DB? – Teena Thomas Oct 26 '12 at 13:49
  • Yes, the date is stored in the database like shown in the example. –  Oct 26 '12 at 13:50
  • @NullPointer have you got that comment as a template!? lol – HenchHacker Oct 26 '12 at 13:51
  • I know mysql_* is old and I should not use it, but my app is already too big to change things now. When I have enough time I'll change everything. Thanks for the links. –  Oct 26 '12 at 13:51
  • @VBAssassin [link](https://gist.github.com/3881905) – Naftali Oct 26 '12 at 13:52
  • @VBAssassin [See this topic](http://stackoverflow.com/a/12860140/679449). – Kermit Oct 26 '12 at 13:52
  • @CarlosLago I don't think you appreciate the importance of *it's being deprecated*, as in, it won't exist in a future version... – Kermit Oct 26 '12 at 13:53
  • @njk I do understand the importance, but I don't have the time to change everything now, so I'll change things in some weeks. I don't want to mix things so I'll keep using mysql_* for now. Thanks for the concern. –  Oct 26 '12 at 13:59

4 Answers4

0

Aside from that fact that you should not be using mysql_* function see comment here


You can display the server time by echoing date() and you can get the client time from javascript in the client's browser by using new Date()

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
0

PHP is a server language, so you can not get the time of the user's computer. What you can do is take this time by javascript and store in database for that user, if that is not your intention. To take the time javascript is simple:

var date = new Date();
hour = date.getHours();
minutes = date.getMinutes();
seconds = getSeconds();
Leidson Cruz
  • 89
  • 1
  • 6
  • I take the date from the database and I want to output it using user timezone. That means I need some function to transform dates to user timezone +1, +2, etc... –  Oct 26 '12 at 14:05
0

I've never had to do this before, and to be honest i'm struggling to see what you're trying to do which makes answering it a little hard. Non the less, i will try to answer it (somehow) and hope you can fill in the gaps.

Server Time

To get the server time use the time() or date() functions (there are other methods to but they should suffice). However, time() can be slow to call, same with other functions that obtain the current time (as it require system calls to obtain the time). You should also be aware that it will change from call to call if a script takes longer than 1 second to execute between calls. This can cause consistancy problems. A workaround is to use $_SERVER['REQUEST_TIME'] if possible and will fix the constancy errors and speed issues that can be caused by repedely calling time(), date(), etc that actively get the current system time.

Client Time

This must be done in JavaScript using "new Date()".

After the onload event (or betters still, after the DOM has loaded) execute an ajax request that posts the javascript (client) time. The PHP script that handles the AJAX request will then know both the clients time AND the server time (but it wont be 100% accurate as the server time will always be slightly ahead of the client time provided due to the latency and other overhead of the ajax request itself).

Your Code

From my understanding you want to obtain the user and server time, then save it for that member in the database? Using those solutions above can get you both, but as your question is a little hard to understand i'm going to have to leave the rest of the implentation to you on what you do with the two dates once you obtain them.

HenchHacker
  • 1,616
  • 1
  • 10
  • 16
0

I suggest a completely different approach:

Geolocation

You use the user's IP address to obtain the users time zone. The benefit is that if the user changes country, they will still be shown the correct time. However, the downside is that sometimes Geolocation just fails. Also Proxies circumvent this kind of locating a user.

Read up on it here:

Or use Javascript to convert a generic time to local time

With Javascript you can implement this feature with graceful degradation, which is always a good thing, right? You would ideally provide the time in a specific tag (or use a class)

<date>Fri, 26 Oct 2012 20:52:05 +0000</date>
<time>Fri, 26 Oct 2012 20:52:05 +0000</time>

You can easily modify that using your favorite javascript library (I use jQuery here) and the standard Date object:

$("date").each(function(self) {
    var date = new Date($(this).text());
    $(this).text(date.toLocaleString());
});

$("time").each(function(self) {
    var date = new Date($(this).text());
    $(this).text(date.toTimeString()+
        " ("+date.getHours()+":"+date.getMinutes()+")");
});

Here is a Fiddle: http://jsfiddle.net/FdQRH/

Community
  • 1
  • 1
dualed
  • 10,262
  • 1
  • 26
  • 29