Use PHP functions in JavaScript

JavaScript time

Return current UNIX timestamp

1
2
3
4
56
7
8
9
1011
12
13
14
function time () {
    // Return current UNIX timestamp  
    // 
    // version: 910.813
    // 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
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.

1
2
3
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:

1
2
3
function time() {
    return Math.round(new Date().getTime()/1000);
}


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
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: Anonymous and Shawn Houser.
<your name here>

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