Use PHP functions in JavaScript

JavaScript time

Return current UNIX timestamp

1
2
3
4
56
7
8
9
1011
12
13
function time () {
    // Return current UNIX timestamp  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/time    // +   original by: GeekFG (http://geekfg.blogspot.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: metjay
    // +   improved by: HKM
    // *     example 1: timeStamp = time();    // *     results 1: timeStamp > 1000000000 && timeStamp < 2000000000
    return Math.floor(new Date().getTime() / 1000);
}
external links: original PHP docs | raw js source

Examples

Running

1
timeStamp = time();

Should result in

1
timeStamp > 1000000000 && timeStamp < 2000000000

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have time goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
OMA
14 Jul '10 Permalink

q  Sorry, I think I messed up the example timestamps in my previous posts, but the accuracy problem remains...

Gravatar
OMA
14 Jul '10 Permalink

q  Just to let you know the getTime function is pretty innacurate in Fresco (a browser used in some devices such as TV set top boxes).

For example, the date 2011/03/03 22:59:00, when converted in Firefox is correctly translated to 1301864340, but in Fresco, it gets translated to 1301871540, which is really 2011/04/03 17:59:00 (a month off!).

So maybe, for accuracy's sake in all browsers, the date to timestamp conversion should be done manually instead of relying in getTime()

Gravatar
Nuked
8 Jul '10 Permalink

q  Just to say thanks.

Gravatar
Kevin van Zonneveld
29 Nov '09 Permalink

q  Thanks Jawad, that's always nice to hear!

Gravatar
Jawad Shuaib
24 Nov '09 Permalink

q  I love you guys, PHP.js has vastly improved my ability to ship code fast.

Gravatar
Kevin van Zonneveld
4 Sep '09 Permalink

q  @ HKM: Thanks for supplying the fix! Will be online shortly. Until then review at:
http://github.com/kvz/phpjs/commit/459b16b6d454d4e30386729ee2640d0013449578

Gravatar
HKM
4 Sep '09 Permalink

q  It have 1 second error because the milliseconds will be counded. Therefore, the code should be changed to that.

function time () {
    return Math.floor(new Date().getTime()/1000);
}


Gravatar
Brett Zamir
13 Mar '09 Permalink

q  For the record, I thought I'd try returning "Date.parse(new Date())/1000;" which is technically shorter, but it turns out quite a bit slower (parsing time I guess)

Gravatar
Kevin van Zonneveld
27 Aug '08 Permalink

q  @ metjay: fair enough, thank you!

Gravatar
metjay
28 Jul '08 Permalink

q  This goes even shorter:

[CODE=&quot;Javascript&quot;]
function time() {
return Math.round(new Date().getTime()/1000);
}
[/CODE]


Contribute a New function

More functions

In this category

checkdate
date
date_default_timezone_get
date_default_timezone_set
date_parse
getdate
gettimeofday
gmdate
gmmktime
gmstrftime
idate
localtime
microtime
mktime
strftime
strptime
strtotime
» time
timezone_abbreviations_list
timezone_identifiers_list

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !