Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { idate } from 'locutus/php/datetime/idate'.
Or with CommonJS: const { idate } = require('locutus/php/datetime/idate')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
idate('y', 1255633200)
9
Here's what our current TypeScript equivalent to PHP's idate looks like.
exportfunctionidate(format?: string, timestamp?: number | string | Date): number { // discuss at: https://locutus.io/php/idate/ // parity verified: PHP 8.3 // original by: Brett Zamir (https://brett-zamir.me) // original by: date // original by: gettimeofday // input by: Alex // bugfixed by: Brett Zamir (https://brett-zamir.me) // improved by: Theriault (https://github.com/Theriault) // example 1: idate('y', 1255633200) // returns 1: 9
if (format === undefined) { thrownewError('idate() expects at least 1 parameter, 0 given') } if (!format.length || format.length > 1) { thrownewError('idate format is one char') }
// @todo: Need to allow date_default_timezone_set() (check for $locutus.default_timezone and use) const _date = typeof timestamp === 'undefined' ? newDate() : timestamp instanceofDate ? newDate(timestamp.getTime()) : newDate(Number(timestamp) * 1000) leta: number
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 original by 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() returnNumber(newDate(a, 0).getTime() - Date.UTC(a, 0) !== newDate(a, 6).getTime() - 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': returnnewDate(_date.getFullYear(), _date.getMonth() + 1, 0).getDate() case'U': returnMath.round(_date.getTime() / 1000) case'w': return _date.getDay() case'W': { const weekDate = newDate(_date.getFullYear(), _date.getMonth(), _date.getDate() - (_date.getDay() || 7) + 3) return1 + Math.round((weekDate.getTime() - newDate(weekDate.getFullYear(), 0, 4).getTime()) / 864e5 / 7) } case'y': returnparseInt((_date.getFullYear() + '').slice(2), 10) // This function returns an integer, unlike _date() case'Y': return _date.getFullYear() case'z': returnMath.floor((_date.getTime() - newDate(_date.getFullYear(), 0, 1).getTime()) / 864e5) case'Z': return -_date.getTimezoneOffset() * 60 default: thrownewError('Unrecognized _date format token') } }
exportfunctionidate(format, timestamp) { // discuss at: https://locutus.io/php/idate/ // parity verified: PHP 8.3 // original by: Brett Zamir (https://brett-zamir.me) // original by: date // original by: gettimeofday // input by: Alex // bugfixed by: Brett Zamir (https://brett-zamir.me) // improved by: Theriault (https://github.com/Theriault) // example 1: idate('y', 1255633200) // returns 1: 9
if (format === undefined) { thrownewError('idate() expects at least 1 parameter, 0 given') } if (!format.length || format.length > 1) { thrownewError('idate format is one char') }
// @todo: Need to allow date_default_timezone_set() (check for $locutus.default_timezone and use) const _date = typeof timestamp === 'undefined' ? newDate() : timestamp instanceofDate ? newDate(timestamp.getTime()) : newDate(Number(timestamp) * 1000) let 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 original by 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() returnNumber(newDate(a, 0).getTime() - Date.UTC(a, 0) !== newDate(a, 6).getTime() - 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': returnnewDate(_date.getFullYear(), _date.getMonth() + 1, 0).getDate() case'U': returnMath.round(_date.getTime() / 1000) case'w': return _date.getDay() case'W': { const weekDate = newDate(_date.getFullYear(), _date.getMonth(), _date.getDate() - (_date.getDay() || 7) + 3) return1 + Math.round((weekDate.getTime() - newDate(weekDate.getFullYear(), 0, 4).getTime()) / 864e5 / 7) } case'y': returnparseInt((_date.getFullYear() + '').slice(2), 10) // This function returns an integer, unlike _date() case'Y': return _date.getFullYear() case'z': returnMath.floor((_date.getTime() - newDate(_date.getFullYear(), 0, 1).getTime()) / 864e5) case'Z': return -_date.getTimezoneOffset() * 60 default: thrownewError('Unrecognized _date format token') } }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.