phpjs
Version:
78 lines (66 loc) • 2.77 kB
Markdown
layout: page
title: "JavaScript getdate function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/getdate:420
- /functions/view/getdate
- /functions/view/420
- /functions/getdate:420
- /functions/420
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's getdate
{% codeblock datetime/getdate.js lang:js https://raw.github.com/kvz/phpjs/master/functions/datetime/getdate.js raw on github %}
function getdate (timestamp) {
// From: http://phpjs.org/functions
// + original by: Paulo Freitas
// + input by: Alex
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: getdate(1055901520);
// * returns 1: {'seconds': 40, 'minutes': 58, 'hours': 21, 'mday': 17, 'wday': 2, 'mon': 6, 'year': 2003, 'yday': 167, 'weekday': 'Tuesday', 'month': 'June', '0': 1055901520}
var _w = ['Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur'];
var _m = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var d = ((typeof timestamp === 'undefined') ? new Date() : // Not provided
(typeof timestamp === 'object') ? new Date(timestamp) : // Javascript Date()
new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
);
var w = d.getDay();
var m = d.getMonth();
var y = d.getFullYear();
var r = {};
r.seconds = d.getSeconds();
r.minutes = d.getMinutes();
r.hours = d.getHours();
r.mday = d.getDate();
r.wday = w;
r.mon = m + 1;
r.year = y;
r.yday = Math.floor((d - (new Date(y, 0, 1))) / 86400000);
r.weekday = _w[w] + 'day';
r.month = _m[m];
r['0'] = parseInt(d.getTime() / 1000, 10);
return r;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/datetime/getdate.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/datetime/getdate.js)
### Example 1
This code
{% codeblock lang:js example %}
getdate(1055901520);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
{'seconds': 40, 'minutes': 58, 'hours': 21, 'mday': 17, 'wday': 2, 'mon': 6, 'year': 2003, 'yday': 167, 'weekday': 'Tuesday', 'month': 'June', '0': 1055901520}
{% endcodeblock %}
### Other PHP functions in the datetime extension
{% render_partial _includes/custom/datetime.html %}