Use PHP functions in JavaScript

JavaScript localtime

Returns the results of the C system call localtime as an associative array if the associative_array argument is set to 1 other wise it is a regular array

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
function localtime(timestamp, is_assoc) {
    // Returns the results of the C system call localtime as an associative array if the associative_array argument is set to 1 other wise it is a regular array  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/localtime    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +  derived from: Josh Fraser (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/)
    // +      parts by: Breaking Par Consulting Inc (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7)
    // +   improved by: Ryan W Tenney (http://ryan.10e.us)
    // *     example 1: localtime();    // *     returns 1: [50,28,0,14,2,109,6,73,0]
    var t, yday, x, o = {};
 
    if (timestamp === undefined) {
        t = new Date();    } else if (timestamp instanceof Date) {
        t = timestamp;
    } else {
        t = new Date(timestamp * 1000);
    } 
    x = function (t, m) {
        var a = (new Date(t.getFullYear(), 0, m, 0, 0, 0, 0)).toUTCString();
        return t - new Date(a.slice(0, a.lastIndexOf(' ') - 1));
    }; 
    yday = Math.floor((t - new Date(t.getFullYear(), 0, 1)) / 86400000);
 
    o = {
        'tm_sec'  : t.getSeconds(),           // seconds        'tm_min'  : t.getMinutes(),           // minutes
        'tm_hour' : t.getHours(),             // hour
        'tm_mday' : t.getDate(),              // day of the month, 1 - 31
        'tm_mon'  : t.getMonth(),             // month of the year, 0 (January) to 11 (December)
        'tm_year' : t.getFullYear() - 1900,   // years since 1900        'tm_wday' : t.getDay(),               // day of the week, 0 (Sun) to 6 (Sat)
        'tm_yday' : yday,                     // day of the year
        'tm_isdst': +(x(t, 1) != x(t, 6))     // is daylight savings time in effect
    };
     return is_assoc ? o : [
        o.tm_sec,
        o.tm_min,
        o.tm_hour,
        o.tm_mday,        o.tm_mon,
        o.tm_year,
        o.tm_wday,
        o.tm_yday,
        o.tm_isdst    ];
}
external links: original PHP docs | raw js source

Examples

Running

1
localtime();

Should return

1
[50,28,0,14,2,109,6,73,0]

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 localtime 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

No comments yet. Be the first!


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: @HalfWinter, Paulo Freitas, Andros Peña Romo, 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 !