views:

146

answers:

4

Is it possible to have JavaScript compute a timestamp returned from PHP's time() function and present it in a readable format such as "Sun, 18th April 2010 at 4:00 pm"?

+3  A: 

Use the Date object to do that:

new Date(<?php echo time(); ?>*1000)

You need to multiply the Unix timestamp by 1000 becuase Date expects the timestamp to be in milliseconds.

And to format the date, you can use this Date.format method (Date has none built in).

Gumbo
That Date.format method is godsend! Thank you so much :)
Lyon
+1  A: 

Here's a function you might use.

Darin Dimitrov
+1  A: 
Pointy
+2  A: 

Instead of a numeric unix timestamp you can also send a textual representation of the date that Date.parse() understands.
Maybe it's just my contribution to global warming but I think there are benefits in using a format that is a bit more human readable and contains the timezone info.

e.g.

<?php
// I have "decided" America/Los_Angeles fits most of my audience
date_default_timezone_set('America/Los_Angeles');
$now = time();
$yesterday = strtotime('yesterday', $now);
// March 27, 1976 08:00:00 tz:America/Los_Angeles
$someotherdate = mktime(8, 0, 0, 3, 27, 1976)
?><html>
  <head><title>...</title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript">
      function foo() {
        $('.datetime').each( function() {
          var t = $(this).text();
          t = new Date(Date.parse(t)).toLocaleString();
          $(this).text(t);
        });
      }
    </script>
  </head>
  <body>
    <div><span class="datetime"><?php echo date(DateTime::RSS, $now); ?></span></div>
    <div><span class="datetime"><?php echo date(DateTime::RSS, $yesterday); ?></span></div>
    <div><span class="datetime"><?php echo date(DateTime::RSS, $someotherdate); ?></span></div>
    <button onclick="foo()">to local time</button>
  </body>
</html>

This prints

Sat, 17 Apr 2010 04:40:15 -0700
Fri, 16 Apr 2010 00:00:00 -0700
Sat, 27 Mar 1976 08:00:00 -0800

in my browser and (since my local timezone is Europe/Berlin, CET, UTC+1/2) after hitting the to local time button

Samstag, 17. April 2010 13:40:15
Freitag, 16. April 2010 09:00:00
Samstag, 27. März 1976 17:00:00
VolkerK
Yes but see my answer. If the server hands the Javascript a time value that's been formatted as per local time *at the server*, then the client will be seeing server-side time. That may be desired, or it may not be; it depends on the application and the meaning of the time value, In any case this approach would yield different results than the approach of using the numeric time value directly.
Pointy
@Pointy: Not sure if I understand your concern in this case. Of course the value depends on what the server's clock "think" the time is, i.e. if the server's clock is wrong the value will be wrong as well. But I used `gmdate()` and `DateTime::W3C` as the format, i.e. the output looks like `2010-04-17T11:12:22+00:00` where `+00:00` marks the timezone (at least the offset). Date.parse() recognizes the offset and treats the value accordingly.
VolkerK
Ok well in that case it would definitely have the same effect as using the "raw" time value (the numeric "number of seconds"), so I'm sorry for failing to see that.
Pointy
@Pointy: Hopefully the revised example is a bit clearer about that part (?)
VolkerK