Use PHP functions in JavaScript

JavaScript idate

Format a local time/date as integer

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
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
function idate(format, timestamp) {
    // Format a local time/date as integer  
    // 
    // version: 911.2217
    // discuss at: http://phpjs.org/functions/idate    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Alex
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Theriault
    // +  derived from: date    // +  derived from: gettimeofday
    // *     example 1: idate('y');
    // *     returns 1: 9
    if (format === undefined) {
        throw 'idate() expects at least 1 parameter, 0 given';    }
    if (!format.length || format.length > 1) {
        throw 'idate format is one char';
    }
     // Fix: Need to allow date_default_timezone_set() (check for this.php_js.default_timezone and use)
    var date = (
        (typeof timestamp === 'undefined') ? new Date() : // Not provided
        (timestamp instanceof Date) ? new Date(timestamp) : // Javascript Date()
        new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)    ), a;
 
    switch (format) {
        case 'B':
            return Math.floor(((date.getUTCHours() * 36e2) + (date.getUTCMinutes() * 60) +                                                date.getUTCSeconds() + 36e2) / 86.4) % 1e3;
        case 'd':
            return date.getDate();
        case 'h':
            return date.getHours() % 12 || 12;        case 'H':
            return date.getHours();
        case 'i':
            return date.getMinutes();
        case 'I': // capital 'i'            // Logic derived from getimeofday().
            // Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC.
            // If they are not equal, then DST is observed.
            a = date.getFullYear();
            return 0 + (((new Date(a, 0)) - Date.UTC(a, 0)) !== ((new Date(a, 6)) - Date.UTC(a, 6)));        case 'L':
            a = date.getFullYear();
            return (!(a & 3) && (a % 1e2 || !(a % 4e2))) ? 1 : 0;
        case 'm':
            return date.getMonth() + 1;        case 's':
            return date.getSeconds();
        case 't':
            return (new Date(date.getFullYear(), date.getMonth() + 1, 0)).getDate();
        case 'U':            return Math.round(date.getTime() / 1000);
        case 'w':
            return date.getDay();
        case 'W':
            a = new Date(date.getFullYear(), date.getMonth(), date.getDate() - (date.getDay() || 7) + 3);            return 1 + Math.round((a - (new Date(a.getFullYear(), 0, 4))) / 864e5 / 7);
        case 'y':
            return parseInt((date.getFullYear() + '').slice(2), 10); // This function returns an integer, unlike date()
        case 'Y':
            return date.getFullYear();        case 'z':
            return Math.floor((date - new Date(date.getFullYear(), 0, 1)) / 864e5);
        case 'Z':
            return -date.getTimezoneOffset() * 60;
        default:            throw 'Unrecognized date format token';
    }
}
external links: original PHP docs | raw js source

Examples

Running

1
idate('y');

Should return

1
9

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 idate 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
Brett Zamir
Jan 20th Permalink

q   JSLint, as good as it is, really needs more configuration options. I would venture a guess that maybe the reason is because the indents at the end are otherwise confusing:

1
2
3
4
56
7
8
9
1011
12
13
switch(v) {
    case 'a':
        break;
    default:
 ...lots of stuff here...
 
 
        break;
   // Might be tempting to put ending bracket here   // or add one here since the indent before this line 
  // is twice as much as the next
}


That being said, I don't like all of JSLint's provisions, including this one, as well as even the one forbidding fall-throughs. There has to be room for different coding styles too. I bet Douglas Crockford may be open to patches which did allow configuration if someone submitted the patches...

Gravatar
Thériault
Jan 19th Permalink

q   For some reason, JSLint wants the cases of a switch statement to be lined up with the switch statement itself:

1
2
3
4
56
switch (expression) {
case 1:
    ...
case 2:
    ...}


...instead of the more readable....

1
2
3
4
56
switch (expression) {
    case 1:
        ...
    case 2:
        ...}


I don't understand why it wants this.


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 !