Skip to content

Instantly share code, notes, and snippets.

@ArrayIterator
Created December 5, 2023 08:52
Show Gist options
  • Save ArrayIterator/2a41d39e49bf8c1e08da71ff58b3e5a4 to your computer and use it in GitHub Desktop.
Save ArrayIterator/2a41d39e49bf8c1e08da71ff58b3e5a4 to your computer and use it in GitHub Desktop.
// stpd! chatgpt 3.5 generated
// phpDateConverter.js
/**
* Converts PHP date format to JavaScript date format.
* @param {string} phpFormat - PHP date format string.
* @returns {string} - JavaScript date format string.
*/
function convertPhpToJsDateFormat(phpFormat) {
// Mapping of PHP date format characters to JavaScript equivalents
const formatMap = {
// Day
'd': 'DD', // Day of the month as zero-padded (01, 02, ..., 31)
'D': 'ddd', // Abbreviated day name (Sun, Mon, ..., Sat)
'j': 'D', // Day of the month without leading zeros (1, 2, ..., 31)
'l': 'dddd', // Full day name (Sunday, Monday, ..., Saturday)
'N': 'E', // ISO-8601 numeric representation of the day of the week (1 for Monday, 7 for Sunday)
'S': '', // English ordinal suffix for the day of the month (st, nd, rd, th)
// Week
'W': 'WW', // ISO-8601 week number of year (weeks starting on Monday)
// Month
'F': 'MMMM', // Full month name (January, February, ..., December)
'm': 'MM', // Numeric representation of a month, with leading zeros (01, 02, ..., 12)
'M': 'MMM', // Abbreviated month name (Jan, Feb, ..., Dec)
'n': 'M', // Numeric representation of a month, without leading zeros (1, 2, ..., 12)
't': '', // Number of days in the given month
// Year
'Y': 'YYYY', // A full numeric representation of a year, 4 digits (e.g., 2023)
'y': 'YY', // A two-digit representation of a year (e.g., 23)
'L': '', // Whether it's a leap year (1 if it is a leap year, 0 otherwise)
'o': 'GGGG', // ISO-8601 year number (similar to 'Y', but if the ISO week number belongs to the previous or next year, it is used)
// Time
'a': 'a', // Lowercase Ante meridiem and Post meridiem (am, pm)
'A': 'A', // Uppercase Ante meridiem and Post meridiem (AM, PM)
'B': '', // Swatch Internet time (000 through 999)
'g': 'h', // 12-hour format of an hour without leading zeros (1, 2, ..., 12)
'G': 'H', // 24-hour format of an hour without leading zeros (0, 1, ..., 23)
'h': 'hh', // 12-hour format of an hour with leading zeros (01, 02, ..., 12)
'H': 'HH', // 24-hour format of an hour with leading zeros (00, 01, ..., 23)
'i': 'mm', // Minutes with leading zeros (00, 01, ..., 59)
's': 'ss', // Seconds with leading zeros (00, 01, ..., 59)
'u': 'SSS', // Microseconds (e.g., 001, 002, ..., 999)
// Timezone
'e': 'zz', // Timezone identifier (e.g., UTC, GMT)
'I': '', // Whether or not the date is in daylight saving time (1 if Daylight Saving Time, 0 otherwise)
'O': 'ZZ', // Difference to Greenwich Mean Time (GMT) in hours (e.g., +0200)
'P': 'Z', // Difference to Greenwich Mean Time (GMT) with colon (e.g., +02:00)
'T': '', // Timezone abbreviation (e.g., EST, MDT)
'Z': '', // Timezone offset in seconds (-43200 through 50400)
// Complete date/time
'c': 'YYYY-MM-DDTHH:mm:ssZ', // ISO 8601 date (2023-12-05T15:30:00Z)
'r': 'ddd, DD MMM YYYY HH:mm:ss ZZ', // RFC 2822 formatted date
// Miscellaneous
'U': 'X' // Seconds since the Unix Epoch (1970-01-01 00:00:00 UTC)
};
return phpFormat.replace(/./g, match => formatMap[match] || match);
}
/**
* Converts PHP date to JavaScript date.
* @param {string | number} phpDate - PHP date string or Unix timestamp.
* @param {string} phpFormat - PHP date format string.
* @returns {string} - JavaScript-formatted date string.
*/
function phpDateToJsDate(phpDate, phpFormat) {
let jsDate;
if (!isNaN(phpDate) && phpDate.toString().length === 10) {
jsDate = new Date(phpDate * 1000);
} else if (!isNaN(phpDate) && phpDate.toString().length === 13) {
jsDate = new Date(phpDate);
} else {
const jsFormat = convertPhpToJsDateFormat(phpFormat);
jsDate = new Date(phpDate);
// Format the JavaScript date using the converted format
const year = jsDate.getFullYear().toString();
const month = (jsDate.getMonth() + 1).toString().padStart(2, '0');
const day = jsDate.getDate().toString().padStart(2, '0');
const hours = jsDate.getHours().toString().padStart(2, '0');
const minutes = jsDate.getMinutes().toString().padStart(2, '0');
const seconds = jsDate.getSeconds().toString().padStart(2, '0');
const milliseconds = jsDate.getMilliseconds().toString().padStart(3, '0');
jsDate = jsFormat
.replace('YYYY', year)
.replace('YY', year.slice(-2))
.replace('MM', month)
.replace('M', jsDate.getMonth() + 1)
.replace('DD', day)
.replace('D', jsDate.getDate())
.replace('HH', hours)
.replace('H', jsDate.getHours())
.replace('hh', hours)
.replace('h', jsDate.getHours() % 12 || 12)
.replace('mm', minutes)
.replace('ss', seconds)
.replace('SSS', milliseconds)
.replace(/a/g, jsDate.getHours() < 12 ? 'am' : 'pm')
.replace(/A/g, jsDate.getHours() < 12 ? 'AM' : 'PM')
.replace(/Z/g, `GMT${-jsDate.getTimezoneOffset() / 60}`)
.replace(/zz/g, jsDate.toTimeString().replace(/.+GMT([+-]\d+).+/, '$1'));
}
return jsDate.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment