phpjs
Version:
74 lines (62 loc) • 2.53 kB
Markdown
layout: page
title: "JavaScript gettimeofday function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/gettimeofday:778
- /functions/view/gettimeofday
- /functions/view/778
- /functions/gettimeofday:778
- /functions/778
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's gettimeofday
{% codeblock datetime/gettimeofday.js lang:js https://raw.github.com/kvz/phpjs/master/functions/datetime/gettimeofday.js raw on github %}
function gettimeofday (return_float) {
// From: http://phpjs.org/functions
// + 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)
// + revised by: Theriault
// * example 1: gettimeofday();
// * returns 1: {sec: 12, usec: 153000, minuteswest: -480, dsttime: 0}
// * example 1: gettimeofday(true);
// * returns 1: 1238748978.49
var t = new Date(),
y = 0;
if (return_float) {
return t.getTime() / 1000;
}
y = t.getFullYear(); // Store current year.
return {
sec: t.getUTCSeconds(),
usec: t.getUTCMilliseconds() * 1000,
minuteswest: t.getTimezoneOffset(),
// Compare Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC to see if DST is observed.
dsttime: 0 + (((new Date(y, 0)) - Date.UTC(y, 0)) !== ((new Date(y, 6)) - Date.UTC(y, 6)))
};
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/datetime/gettimeofday.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/gettimeofday.js)
### Example 1
This code
{% codeblock lang:js example %}
gettimeofday();
gettimeofday(true);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
{sec: 12, usec: 153000, minuteswest: -480, dsttime: 0}
1238748978.49
{% endcodeblock %}
### Other PHP functions in the datetime extension
{% render_partial _includes/custom/datetime.html %}