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 53 54 5556 57 | 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: 909.322 // 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) // * example 1: localtime(); // * returns 1: [50,28,0,14,2,109,6,73,0] if (timestamp === undefined) { timestamp = Math.round(new Date().getTime()/1000); // this.time() } else if (timestamp instanceof Date) { timestamp = timestamp/1000; // Let it work with JavaScript data objects without the need for conversion } var t = new Date(timestamp*1000); // Calculate day of year var jan1 = new Date(t.getFullYear(), 0, 1); var yday = Math.ceil((t - jan1) / 86400000)-1; // Calculate Daylight Saving Time jan1 = new Date(t.getFullYear(), 0, 1, 0, 0, 0, 0); // jan 1st var june1 = new Date(t.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st var temp = jan1.toUTCString(); var jan2 = new Date(temp.slice(0, temp.lastIndexOf(' ')-1)); temp = june1.toUTCString(); var june2 = new Date(temp.slice(0, temp.lastIndexOf(' ')-1)); var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60); var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60); var dst; if (std_time_offset === daylight_time_offset) { dst = 0; // daylight savings time is NOT observed } else { // positive is southern, negative is northern hemisphere var hemisphere = std_time_offset - daylight_time_offset; if (hemisphere >= 0) { std_time_offset = daylight_time_offset; } dst = 1; // daylight savings time is observed } if (is_assoc) { return { 'tm_sec': t.getSeconds(), // seconds 'tm_min': t.getMinutes(), // minutes 'tm_hour': t.getHours(),// hour 'tm_mday': t.getDate(),// day of the month Months are from 0 (Jan) to 11 (Dec) and days of the week are from 0 (Sun) to 6 (Sat). 'tm_mon': t.getMonth(),// month of the year, starting with 0 for January 'tm_year': t.getFullYear()-1900,// Years since 1900 'tm_wday': t.getDay(),// Day of the week 'tm_yday': yday,// Day of the year 'tm_isdst': dst// Is daylight savings time in effect }; } return [t.getSeconds(), t.getMinutes(), t.getHours(), t.getDate(), t.getMonth(), t.getFullYear()-1900, t.getDay(), yday, dst]; } |
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.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>