PHP's gmdate in TypeScript

✓ Verified: PHP 8.3
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 { gmdate } from 'locutus/php/datetime/gmdate'.

Or with CommonJS: const { gmdate } = require('locutus/php/datetime/gmdate')

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.

#codeexpected result
1gmdate('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400); // Return will depend on your timezone'07:09:40 m is month'

Dependencies

This function uses the following Locutus functions:

  • date (php/datetime)

Here's what our current TypeScript equivalent to PHP's gmdate looks like.

import { date } from '../datetime/date.ts'

export function gmdate(format: string, timestamp?: string | number | Date): string {
// discuss at: https://locutus.io/php/gmdate/
// parity verified: PHP 8.3
// original by: Brett Zamir (https://brett-zamir.me)
// input by: Alex
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// example 1: gmdate('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400); // Return will depend on your timezone
// returns 1: '07:09:40 m is month'

const dt =
typeof timestamp === 'undefined'
? new Date() // Not provided
: timestamp instanceof Date
? new Date(timestamp) // Javascript Date()
: new Date(Number(timestamp) * 1000) // UNIX timestamp (auto-convert to int)

const ts = Date.parse(dt.toUTCString().slice(0, -4)) / 1000

return date(format, ts)
}

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.

View on GitHub · Edit on GitHub · View Raw


« More PHP datetime functions


Star