164cf7cc6SAndreas Gohr<?php 264cf7cc6SAndreas Gohr 317a3a578SAndreas Gohr// phpcs:disable Generic.Files.LineLength.TooLong 417a3a578SAndreas Gohr 564cf7cc6SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 664cf7cc6SAndreas Gohr 764cf7cc6SAndreas Gohr/** 864cf7cc6SAndreas Gohr * Class DateFormatConverter 964cf7cc6SAndreas Gohr * 1064cf7cc6SAndreas Gohr * Allows conversion between the two format strings used in PHP. Not all placeholders are available in both 1164cf7cc6SAndreas Gohr * formats. The conversion tries will use similar but not exactly the same placeholders if possible. When no suitable 1264cf7cc6SAndreas Gohr * replacement can be found, the placeholder is removed. 1364cf7cc6SAndreas Gohr * 1464cf7cc6SAndreas Gohr * Do not use this where formats are used in creating machine readable data (like feeds, APIs whatever). This is 1564cf7cc6SAndreas Gohr * only meant for cases where human read output is created. 1664cf7cc6SAndreas Gohr * 1764cf7cc6SAndreas Gohr * @package dokuwiki\plugin\struct\meta 1864cf7cc6SAndreas Gohr */ 19d6d97f60SAnna Dabrowskaclass DateFormatConverter 20d6d97f60SAnna Dabrowska{ 21*7234bfb1Ssplitbrain protected static $strftime = [ 2264cf7cc6SAndreas Gohr // Day 23*7234bfb1Ssplitbrain '%a' => 'D', 24*7234bfb1Ssplitbrain // An abbreviated textual representation of the day Sun through Sat 25*7234bfb1Ssplitbrain '%A' => 'l', 26*7234bfb1Ssplitbrain // A full textual representation of the day Sunday through Saturday 27*7234bfb1Ssplitbrain '%d' => 'd', 28*7234bfb1Ssplitbrain // Two-digit day of the month (with leading zeros) 01 to 31 29*7234bfb1Ssplitbrain '%e' => 'j', 30*7234bfb1Ssplitbrain // Day of the month, with a space preceding single digits. Not implemented as described on Windows. See below for more information. 1 to 31 31*7234bfb1Ssplitbrain '%j' => '', 32*7234bfb1Ssplitbrain // NOT SUPPORTED Day of the year, 3 digits with leading zeros 001 to 366 33*7234bfb1Ssplitbrain '%u' => 'N', 34*7234bfb1Ssplitbrain // ISO-8601 numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday) 35*7234bfb1Ssplitbrain '%w' => 'w', 36*7234bfb1Ssplitbrain // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) 3764cf7cc6SAndreas Gohr // Week 38*7234bfb1Ssplitbrain '%U' => '', 39*7234bfb1Ssplitbrain // NOT SUPPORTED Week number of the given year, starting with the first Sunday as the first week 13 (for the 13th full week of the year) 40*7234bfb1Ssplitbrain '%V' => 'W', 41*7234bfb1Ssplitbrain // ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week 01 through 53 (where 53 accounts for an overlapping week) 42*7234bfb1Ssplitbrain '%W' => '', 43*7234bfb1Ssplitbrain // NOT SUPPORTED A numeric representation of the week of the year, starting with the first Monday as the first week 46 (for the 46th week of the year beginning with a Monday) 4464cf7cc6SAndreas Gohr // Month 45*7234bfb1Ssplitbrain '%b' => 'M', 46*7234bfb1Ssplitbrain // Abbreviated month name, based on the locale Jan through Dec 47*7234bfb1Ssplitbrain '%B' => 'F', 48*7234bfb1Ssplitbrain // Full month name, based on the locale January through December 49*7234bfb1Ssplitbrain '%h' => 'M', 50*7234bfb1Ssplitbrain // Abbreviated month name, based on the locale (an alias of %b) Jan through Dec 51*7234bfb1Ssplitbrain '%m' => 'm', 52*7234bfb1Ssplitbrain // Two digit representation of the month 01 (for January) through 12 (for December) 5364cf7cc6SAndreas Gohr // Year 54*7234bfb1Ssplitbrain '%C' => '', 55*7234bfb1Ssplitbrain // NOT SUPPORTED Two digit representation of the century (year divided by 100, truncated to an integer) 19 for the 20th Century 56*7234bfb1Ssplitbrain '%g' => 'y', 57*7234bfb1Ssplitbrain // Two digit representation of the year going by ISO-8601:1988 standards (see %V) Example: 09 for the week of January 6, 2009 58*7234bfb1Ssplitbrain '%G' => 'Y', 59*7234bfb1Ssplitbrain // The full four-digit version of %g Example: 2008 for the week of January 3, 2009 60*7234bfb1Ssplitbrain '%y' => 'y', 61*7234bfb1Ssplitbrain // Two digit representation of the year Example: 09 for 2009, 79 for 1979 62*7234bfb1Ssplitbrain '%Y' => 'Y', 63*7234bfb1Ssplitbrain // Four digit representation for the year Example: 2038 6464cf7cc6SAndreas Gohr // Time 65*7234bfb1Ssplitbrain '%H' => 'H', 66*7234bfb1Ssplitbrain // Two digit representation of the hour in 24-hour format 00 through 23 67*7234bfb1Ssplitbrain '%k' => 'G', 68*7234bfb1Ssplitbrain // Two digit representation of the hour in 24-hour format, with a space preceding single digits 0 through 23 69*7234bfb1Ssplitbrain '%I' => 'h', 70*7234bfb1Ssplitbrain // Two digit representation of the hour in 12-hour format 01 through 12 71*7234bfb1Ssplitbrain '%l' => 'g', 72*7234bfb1Ssplitbrain // (lower-case 'L') Hour in 12-hour format, with a space preceding single digits 1 through 12 73*7234bfb1Ssplitbrain '%M' => 'i', 74*7234bfb1Ssplitbrain // Two digit representation of the minute 00 through 59 75*7234bfb1Ssplitbrain '%p' => 'A', 76*7234bfb1Ssplitbrain // UPPER-CASE 'AM' or 'PM' based on the given time Example: AM for 00:31, PM for 22:23 77*7234bfb1Ssplitbrain '%P' => 'a', 78*7234bfb1Ssplitbrain // lower-case 'am' or 'pm' based on the given time Example: am for 00:31, pm for 22:23 79*7234bfb1Ssplitbrain '%r' => 'h:i:s A', 80*7234bfb1Ssplitbrain // Same as %I:%M:%S %p Example: 09:34:17 PM for 21:34:17 81*7234bfb1Ssplitbrain '%R' => 'H:i', 82*7234bfb1Ssplitbrain // Same as %H:%M Example: 00:35 for 12:35 AM, 16:44for 4:44 PM 83*7234bfb1Ssplitbrain '%S' => 's', 84*7234bfb1Ssplitbrain // Two digit representation of the second 00 through 59 85*7234bfb1Ssplitbrain '%T' => 'H:i:s', 86*7234bfb1Ssplitbrain // Same as %H:%M:%S Example: 21:34:17 for 09:34:17 PM 87*7234bfb1Ssplitbrain '%X' => 'H:i:s', 88*7234bfb1Ssplitbrain // Preferred time representation based on locale, without the date Example: 03:59:16 or 15:59:16 89*7234bfb1Ssplitbrain '%z' => 'z', 90*7234bfb1Ssplitbrain // The time zone offset. Not implemented as described on Windows. See below for more information. Example: -0500 for US Eastern Time 91*7234bfb1Ssplitbrain '%Z' => 'T', 92*7234bfb1Ssplitbrain // The time zone abbreviation. Not implemented as described on Windows. See below for more information. Example: EST for Eastern Time 9364cf7cc6SAndreas Gohr // Time and Date Stamps 94*7234bfb1Ssplitbrain '%c' => 'D M j H:i:s Y', 95*7234bfb1Ssplitbrain // Preferred date and time stamp based on locale Example: Tue Feb 5 00:45:10 2009 for February 5, 2009 at 12:45:10 AM 96*7234bfb1Ssplitbrain '%D' => 'm/d/y', 97*7234bfb1Ssplitbrain // Same as %m/%d/%y Example: 02/05/09 for February 5, 2009 98*7234bfb1Ssplitbrain '%F' => 'Y/m/d', 99*7234bfb1Ssplitbrain // Same as %Y-%m-%d (commonly used in database datestamps) Example: 2009-02-05 for February 5, 2009 100*7234bfb1Ssplitbrain '%s' => 'U', 101*7234bfb1Ssplitbrain // Unix Epoch Time timestamp (same as the time() function) Example: 305815200 for September 10, 1979 08:40:00 AM 102*7234bfb1Ssplitbrain '%x' => 'm/d/y', 103*7234bfb1Ssplitbrain // Preferred date representation based on locale, without the time Example: 02/05/09 for February 5, 2009 10464cf7cc6SAndreas Gohr // Miscellaneous 105*7234bfb1Ssplitbrain '%n' => "\n", 106*7234bfb1Ssplitbrain // A newline character (\n) --- 107*7234bfb1Ssplitbrain '%t' => "\t", 108*7234bfb1Ssplitbrain // A Tab character (\t) --- 109*7234bfb1Ssplitbrain '%%' => '%', 110*7234bfb1Ssplitbrain ]; 11164cf7cc6SAndreas Gohr 112*7234bfb1Ssplitbrain protected static $date = [ 11364cf7cc6SAndreas Gohr // Day 114*7234bfb1Ssplitbrain 'd' => '%d', 115*7234bfb1Ssplitbrain // Day of the month, 2 digits with leading zeros 01 to 31 116*7234bfb1Ssplitbrain 'D' => '%a', 117*7234bfb1Ssplitbrain // A textual representation of a day, three letters Mon through Sun 118*7234bfb1Ssplitbrain 'j' => '%e', 119*7234bfb1Ssplitbrain // Day of the month without leading zeros 1 to 31 120*7234bfb1Ssplitbrain 'l' => '%A', 121*7234bfb1Ssplitbrain // (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday 122*7234bfb1Ssplitbrain 'N' => '%u', 123*7234bfb1Ssplitbrain // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday) 124*7234bfb1Ssplitbrain 'S' => '', 125*7234bfb1Ssplitbrain // NOT SUPPORTED English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j 126*7234bfb1Ssplitbrain 'w' => '%w', 127*7234bfb1Ssplitbrain // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) 128*7234bfb1Ssplitbrain 'z' => '', 129*7234bfb1Ssplitbrain // NOT SUPPORTED The day of the year (starting from 0) 0 through 365 13064cf7cc6SAndreas Gohr // Week 131*7234bfb1Ssplitbrain 'W' => '%V', 132*7234bfb1Ssplitbrain // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year) 13364cf7cc6SAndreas Gohr // Month 134*7234bfb1Ssplitbrain 'F' => '%B', 135*7234bfb1Ssplitbrain // A full textual representation of a month, such as January or March January through December 136*7234bfb1Ssplitbrain 'm' => '%m', 137*7234bfb1Ssplitbrain // Numeric representation of a month, with leading zeros 01 through 12 138*7234bfb1Ssplitbrain 'M' => '%b', 139*7234bfb1Ssplitbrain // A short textual representation of a month, three letters Jan through Dec 140*7234bfb1Ssplitbrain 'n' => '%m', 141*7234bfb1Ssplitbrain // Numeric representation of a month, without leading zeros 1 through 12 142*7234bfb1Ssplitbrain 't' => '', 143*7234bfb1Ssplitbrain // NOT SUPPORTED Number of days in the given month 28 through 31 14464cf7cc6SAndreas Gohr // Year 145*7234bfb1Ssplitbrain 'L' => '', 146*7234bfb1Ssplitbrain // NOT SUPPORTED Whether it's a leap year 1 if it is a leap year, 0 otherwise. 147*7234bfb1Ssplitbrain 'o' => '%g', 148*7234bfb1Ssplitbrain // ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) Examples: 1999or 2003 149*7234bfb1Ssplitbrain 'Y' => '%Y', 150*7234bfb1Ssplitbrain // A full numeric representation of a year, 4 digits Examples: 1999or 2003 151*7234bfb1Ssplitbrain 'y' => '%y', 152*7234bfb1Ssplitbrain // A two digit representation of a year Examples: 99 or03 15364cf7cc6SAndreas Gohr // Time 154*7234bfb1Ssplitbrain 'a' => '%P', 155*7234bfb1Ssplitbrain // Lowercase Ante meridiem and Post meridiem am or pm 156*7234bfb1Ssplitbrain 'A' => '%p', 157*7234bfb1Ssplitbrain // Uppercase Ante meridiem and Post meridiem AM or PM 158*7234bfb1Ssplitbrain 'B' => '', 159*7234bfb1Ssplitbrain // NOT SUPPORTED Swatch Internet time 000 through 999 160*7234bfb1Ssplitbrain 'g' => '%l', 161*7234bfb1Ssplitbrain // 12-hour format of an hour without leading zeros 1 through 12 162*7234bfb1Ssplitbrain 'G' => '%k', 163*7234bfb1Ssplitbrain // 24-hour format of an hour without leading zeros 0 through 23 164*7234bfb1Ssplitbrain 'h' => '%I', 165*7234bfb1Ssplitbrain // 12-hour format of an hour with leading zeros 01 through 12 166*7234bfb1Ssplitbrain 'H' => '%H', 167*7234bfb1Ssplitbrain // 24-hour format of an hour with leading zeros 00 through 23 168*7234bfb1Ssplitbrain 'i' => '%M', 169*7234bfb1Ssplitbrain // Minutes with leading zeros 00 to 59 170*7234bfb1Ssplitbrain 's' => '%S', 171*7234bfb1Ssplitbrain // Seconds, with leading zeros 00 through 59 172*7234bfb1Ssplitbrain 'u' => '%s000000', 173*7234bfb1Ssplitbrain // Microseconds (added in PHP 5.2.2). Note that date() will always generate000000 since it takes an integer parameter, whereas DateTime::format()does support microseconds if DateTime was created with microseconds. Example: 654321 17464cf7cc6SAndreas Gohr // Timezone 175*7234bfb1Ssplitbrain 'e' => '%Z', 176*7234bfb1Ssplitbrain // Timezone identifier (added in PHP 5.1.0) Examples: UTC,GMT,Atlantic/Azores 177*7234bfb1Ssplitbrain 'I' => '', 178*7234bfb1Ssplitbrain // NOT SUPPORTED (capital i) Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0otherwise. 179*7234bfb1Ssplitbrain 'O' => '%z', 180*7234bfb1Ssplitbrain // Difference to Greenwich time (GMT) in hours Example: +0200 181*7234bfb1Ssplitbrain 'P' => '%z', 182*7234bfb1Ssplitbrain // Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) Example: +02:00 183*7234bfb1Ssplitbrain 'T' => '%Z', 184*7234bfb1Ssplitbrain // Timezone abbreviation Examples: EST,MDT ... 185*7234bfb1Ssplitbrain 'Z' => '', 186*7234bfb1Ssplitbrain // NOT SUPPORTED Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through50400 18764cf7cc6SAndreas Gohr // Full Date/Time 188*7234bfb1Ssplitbrain 'c' => '', 189*7234bfb1Ssplitbrain // NOT SUPPORTED ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00 190*7234bfb1Ssplitbrain 'r' => '%a, %e %b %Y %H:%M:%S %s', 191*7234bfb1Ssplitbrain // » RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200 192*7234bfb1Ssplitbrain 'U' => '%s', 193*7234bfb1Ssplitbrain ]; 19464cf7cc6SAndreas Gohr 19564cf7cc6SAndreas Gohr /** 19664cf7cc6SAndreas Gohr * Convert a strftime format string to a date format string 19764cf7cc6SAndreas Gohr * 19864cf7cc6SAndreas Gohr * @param string $strftime 19964cf7cc6SAndreas Gohr * @return string 20064cf7cc6SAndreas Gohr */ 201d6d97f60SAnna Dabrowska public static function toDate($strftime) 202d6d97f60SAnna Dabrowska { 20341d0641dSAndreas Gohr $date = $strftime; 20441d0641dSAndreas Gohr 20541d0641dSAndreas Gohr /* All characters that are not strftime placeholders need to be escaped */ 20641d0641dSAndreas Gohr { 20741d0641dSAndreas Gohr $datekeys = array_keys(self::$date); 20841d0641dSAndreas Gohr // create negative lookbehind regex to match all known date chars that are not a strtime pattern now 20941d0641dSAndreas Gohr $from = array_map( 21041d0641dSAndreas Gohr function ($in) { 21141d0641dSAndreas Gohr return '/(?<!%)' . $in . '/'; 21241d0641dSAndreas Gohr }, 21341d0641dSAndreas Gohr $datekeys 21441d0641dSAndreas Gohr ); 21541d0641dSAndreas Gohr // those need to be escaped 21641d0641dSAndreas Gohr $to = array_map( 21741d0641dSAndreas Gohr function ($in) { 21841d0641dSAndreas Gohr return '\\' . $in; 21941d0641dSAndreas Gohr }, 22041d0641dSAndreas Gohr $datekeys 22141d0641dSAndreas Gohr ); 22241d0641dSAndreas Gohr // escape date chars 22341d0641dSAndreas Gohr $date = preg_replace($from, $to, $date); 22441d0641dSAndreas Gohr } 22541d0641dSAndreas Gohr 22641d0641dSAndreas Gohr /* strftime to date conversion */ 22741d0641dSAndreas Gohr { 22864cf7cc6SAndreas Gohr $date = str_replace( 22964cf7cc6SAndreas Gohr array_keys(self::$strftime), 23064cf7cc6SAndreas Gohr array_values(self::$strftime), 23141d0641dSAndreas Gohr $date 23264cf7cc6SAndreas Gohr ); 23341d0641dSAndreas Gohr } 23441d0641dSAndreas Gohr 23564cf7cc6SAndreas Gohr return $date; 23664cf7cc6SAndreas Gohr } 23764cf7cc6SAndreas Gohr 23864cf7cc6SAndreas Gohr /** 23964cf7cc6SAndreas Gohr * Convert a date format string to a strftime format string 24064cf7cc6SAndreas Gohr * 24164cf7cc6SAndreas Gohr * @param string $date 24264cf7cc6SAndreas Gohr * @return string 24364cf7cc6SAndreas Gohr */ 244d6d97f60SAnna Dabrowska public static function toStrftime($date) 245d6d97f60SAnna Dabrowska { 24641d0641dSAndreas Gohr /* date to strftime conversion */ 24741d0641dSAndreas Gohr { 24841d0641dSAndreas Gohr // create negative lookbehind regex to match all unescaped known chars 24941d0641dSAndreas Gohr $from = array_keys(self::$date); 25041d0641dSAndreas Gohr $from = array_map( 25141d0641dSAndreas Gohr function ($in) { 25241d0641dSAndreas Gohr return '/(?<!\\\\)' . $in . '/'; 25341d0641dSAndreas Gohr }, 25441d0641dSAndreas Gohr $from 25564cf7cc6SAndreas Gohr ); 25641d0641dSAndreas Gohr $to = array_values(self::$date); 25741d0641dSAndreas Gohr 25841d0641dSAndreas Gohr // percents need escaping: 25941d0641dSAndreas Gohr array_unshift($from, '/%/'); 26041d0641dSAndreas Gohr array_unshift($to, '%%'); 26141d0641dSAndreas Gohr 26241d0641dSAndreas Gohr // replace all the placeholders 26341d0641dSAndreas Gohr $strftime = preg_replace($from, $to, $date); 26441d0641dSAndreas Gohr } 26541d0641dSAndreas Gohr 26641d0641dSAndreas Gohr /* unescape date escapes */ 26741d0641dSAndreas Gohr { 26841d0641dSAndreas Gohr $datekeys = array_keys(self::$date); 26941d0641dSAndreas Gohr $from = array_map( 27041d0641dSAndreas Gohr function ($in) { 27141d0641dSAndreas Gohr return '/\\\\' . $in . '/'; 27241d0641dSAndreas Gohr }, 27341d0641dSAndreas Gohr $datekeys 27441d0641dSAndreas Gohr ); 27541d0641dSAndreas Gohr $strftime = preg_replace($from, $datekeys, $strftime); 27641d0641dSAndreas Gohr } 27741d0641dSAndreas Gohr 27864cf7cc6SAndreas Gohr return $strftime; 27964cf7cc6SAndreas Gohr } 28064cf7cc6SAndreas Gohr} 281