PHP's mktime in JavaScript

Here’s what our current JavaScript equivalent to PHP's mktime looks like.

module.exports = functionmktime () {
// discuss at: https://locutus.io/php/mktime/
// original by: Kevin van Zonneveld (https://kvz.io)
// improved by: baris ozdil
// improved by: Kevin van Zonneveld (https://kvz.io)
// improved by: FGFEmperor
// improved by: Brett Zamir (https://brett-zamir.me)
// input by: gabriel paderni
// input by: Yannoo
// input by: jakes
// input by: 3D-GRAF
// input by: Chris
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Marc Palau
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// revised by: Theriault (https://github.com/Theriault)
// note 1: The return values of the following examples are
// note 1: received only if your system's timezone is UTC.
// example 1: mktime(14, 10, 2, 2, 1, 2008)
// returns 1: 1201875002
// example 2: mktime(0, 0, 0, 0, 1, 2008)
// returns 2: 1196467200
// example 3: var $make = mktime()
// example 3: var $td = new Date()
// example 3: var $real = Math.floor($td.getTime() / 1000)
// example 3: var $diff = ($real - $make)
// example 3: $diff < 5
// returns 3: true
// example 4: mktime(0, 0, 0, 13, 1, 1997)
// returns 4: 883612800
// example 5: mktime(0, 0, 0, 1, 1, 1998)
// returns 5: 883612800
// example 6: mktime(0, 0, 0, 1, 1, 98)
// returns 6: 883612800
// example 7: mktime(23, 59, 59, 13, 0, 2010)
// returns 7: 1293839999
// example 8: mktime(0, 0, -1, 1, 1, 1970)
// returns 8: -1
const d = newDate()
const r = arguments
let i = 0
const e = ['Hours', 'Minutes', 'Seconds', 'Month', 'Date', 'FullYear']
for (i = 0; i < e.length; i++) {
if (typeof r[i] === 'undefined') {
r[i] = d['get' + e[i]]()
// +1 to fix JS months.
r[i] += (i === 3)
} else {
r[i] = parseInt(r[i], 10)
if (isNaN(r[i])) {
returnfalse
}
}
}
// Map years 0-69 to 2000-2069 and years 70-100 to 1970-2000.
r[5] += (r[5] >= 0 ? (r[5] <= 69 ? 2e3 : (r[5] <= 100 ? 1900 : 0)) : 0)
// Set year, month (-1 to fix JS months), and date.
// !This must come before the call to setHours!
d.setFullYear(r[5], r[3] - 1, r[4])
// Set hours, minutes, and seconds.
d.setHours(r[0], r[1], r[2])
const time = d.getTime()
// Divide milliseconds by 1000 to return seconds and drop decimal.
// Add 1 second if negative or it'll be off from PHP by 1 second.
return (time / 1e3 >> 0) - (time < 0)
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/datetime/mktime'). You could also require the datetime module in full so that you could access datetime.mktime instead.

If you intend to target the browser, you can then use a module bundler such as Parcel, webpack, Browserify, or rollup.js. This can be important because Locutus allows modern JavaScript in the source files, meaning it may not work in all browsers without a build/transpile step. Locutus does transpile all functions to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • The return values of the following examples are received only if your system’s timezone is UTC.

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.

#codeexpected result
1mktime(14, 10, 2, 2, 1, 2008)1201875002
2mktime(0, 0, 0, 0, 1, 2008)1196467200
3var $make = mktime() var $td = new Date() var $real = Math.floor($td.getTime() / 1000) var $diff = ($real - $make) $diff < 5true
4mktime(0, 0, 0, 13, 1, 1997)883612800
5mktime(0, 0, 0, 1, 1, 1998)883612800
6mktime(0, 0, 0, 1, 1, 98)883612800
7mktime(23, 59, 59, 13, 0, 2010)1293839999
8mktime(0, 0, -1, 1, 1, 1970)-1

« More PHP datetime functions


Star