views:

8231

answers:

7

I am storing time in a MySQL database as a Unix timestamp and that gets sent to some Javascript, how would I get just the time out of it? Ex. HH/MM/SS

A: 

Take a look at PHP's date function.

Ionuț G. Stan
If you're down-voting me because my solution is in PHP, then take a look a second look at the question. He's not asking it to be in JavaScript. Furthermore, there's a PHP tag in there.
Ionuț G. Stan
@Ionut G. Stan: "...and that gets sent to some Javascript, how would I get just the time out of it?". It sounds to me like he wants the answer in JavaScript. The only place PHP is even mentioned is in a (in my opinion, misplaced) tag. Finally, he's marked one of the JavaScript answers as his accepted answer.
Steve Harrison
@Steve, in my opinion, it could have been MySQL as well. He didn't mentioned the place where he wants the transformation and at the time I answered, I couldn't have a clue what kind of answer he'll accept.
Ionuț G. Stan
@Ionut G. Stan: Yes, I agree that the question is a bit ambiguous.
Steve Harrison
+13  A: 
// create a new javascript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(unix_timestamp*1000);
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();

// will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes + ':' + seconds;

For more information regarding the Date object, please refer to this page on W3Schools or this page from the JavaScript 1.5 reference.

Aron Rotteveel
UNIX timestamps are expressed in seconds—but JavaScript works in milliseconds! I don't think the above code will work correctly.
Steve Harrison
Corrected, thanks.
Aron Rotteveel
also you could save yourself a couple of processor ticks by avoiding the need for the large multiplication by just appending three 0s:echo "var date = new Date(" . $timestamp . "000);\n";
nickf
+11  A: 

JavaScript works in milliseconds, so you'll first have to convert the UNIX timestamp from seconds to milliseconds.

var date = new Date([UNIX Timestamp] * 1000);
// Manipulate JavaScript Date object here...

Steve

Steve Harrison
+4  A: 

UNIX timestamp is number of seconds since 00:00:00 UTC on January 1, 1970 (according to Wikipedia).

Argument of Date object in Javascript is number of miliseconds since 00:00:00 UTC on January 1, 1970 (according to W3C Javascript documentation).

See code below for example:

    function tm(unix_tm) {
     var dt = new Date(unix_tm*1000);
     document.writeln(dt.getHours() + '/' + dt.getMinutes() + '/' + dt.getSeconds() + ' -- ' + dt + '<br>');

    }

tm(60);
tm(86400);

gives:

1/1/0 -- Thu Jan 01 1970 01:01:00 GMT+0100 (Central European Standard Time)
1/0/0 -- Fri Jan 02 1970 01:00:00 GMT+0100 (Central European Standard Time)
Grzegorz Gierlik
A: 

Mysql from_unixtime function does the job:

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(), '%h/%i/%s');

For timestamps from getTimeMillis() I had to divide the timestamp by 1000 to get the right result.

Vladiat0r