PHP's mktime in JavaScript
How to use
You you can install via yarn add locutus
and
require this function via const mktime = require('locutus/php/datetime/mktime')
.
It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.
Examples
Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.
# | code | expected result |
---|---|---|
1 | mktime(14, 10, 2, 2, 1, 2008) | 1201875002 |
2 | mktime(0, 0, 0, 0, 1, 2008) | 1196467200 |
3 | var $make = mktime()
var $td = new Date()
var $real = Math.floor($td.getTime() / 1000)
var $diff = ($real - $make)
$diff < 5 | true |
4 | mktime(0, 0, 0, 13, 1, 1997) | 883612800 |
5 | mktime(0, 0, 0, 1, 1, 1998) | 883612800 |
6 | mktime(0, 0, 0, 1, 1, 98) | 883612800 |
7 | mktime(23, 59, 59, 13, 0, 2010) | 1293839999 |
8 | mktime(0, 0, -1, 1, 1, 1970) | -1 |
Notes
The return values of the following examples are received only if your system’s timezone is UTC.
Here’s what our current JavaScript equivalent to PHP's mktime looks like.
module.exports = function mktime() { |
A community effort
Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows
The McDonald’s Theory.
This means that we assimilate first iterations with imperfections,
hoping for others to take issue with-and improve them.
This unorthodox approach has worked very well to foster fun and fruitful collaboration,
but please be reminded to use our creations at your own risk.
THE SOFTWARE IS PROVIDED "AS IS"
has never been more true than for Locutus.
Now go and: [ View on GitHub | Edit on GitHub | View Raw ]
Star