PHP's gmstrftime 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 { gmstrftime } from 'locutus/php/datetime/gmstrftime'.

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

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
1gmstrftime("%A", 1062462400)'Tuesday'

Dependencies

This function uses the following Locutus functions:

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

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

export function gmstrftime(format: string, timestamp?: number | Date): string {
// discuss at: https://locutus.io/php/gmstrftime/
// 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: gmstrftime("%A", 1062462400)
// returns 1: 'Tuesday'

const date =
typeof timestamp === 'undefined'
? new Date()
: timestamp instanceof Date
? new Date(timestamp)
: new Date(timestamp * 1000)

const utcTimestamp = Date.parse(date.toUTCString().slice(0, -4)) / 1000

return strftime(format, utcTimestamp)
}

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