1 | <?php |
||
9 | class DateFormatHelper |
||
10 | { |
||
11 | const KEY_GENERAL = 'general'; |
||
12 | const KEY_HOUR_12 = '12h'; |
||
13 | const KEY_HOUR_24 = '24h'; |
||
14 | |||
15 | /** |
||
16 | * This map is used to replace Excel format characters by their PHP equivalent. |
||
17 | * Keys should be ordered from longest to smallest. |
||
18 | * |
||
19 | * @var array Mapping between Excel format characters and PHP format characters |
||
20 | */ |
||
21 | private static $excelDateFormatToPHPDateFormatMapping = [ |
||
22 | self::KEY_GENERAL => [ |
||
23 | // Time |
||
24 | 'am/pm' => 'A', // Uppercase Ante meridiem and Post meridiem |
||
25 | ':mm' => ':i', // Minutes with leading zeros - if preceded by a ":" (otherwise month) |
||
26 | 'mm:' => 'i:', // Minutes with leading zeros - if followed by a ":" (otherwise month) |
||
27 | 'ss' => 's', // Seconds, with leading zeros |
||
28 | '.s' => '', // Ignore (fractional seconds format does not exist in PHP) |
||
29 | |||
30 | // Date |
||
31 | 'e' => 'Y', // Full numeric representation of a year, 4 digits |
||
32 | 'yyyy' => 'Y', // Full numeric representation of a year, 4 digits |
||
33 | 'yy' => 'y', // Two digit representation of a year |
||
34 | 'mmmmm' => 'M', // Short textual representation of a month, three letters ("mmmmm" should only contain the 1st letter...) |
||
35 | 'mmmm' => 'F', // Full textual representation of a month |
||
36 | 'mmm' => 'M', // Short textual representation of a month, three letters |
||
37 | 'mm' => 'm', // Numeric representation of a month, with leading zeros |
||
38 | 'm' => 'n', // Numeric representation of a month, without leading zeros |
||
39 | 'dddd' => 'l', // Full textual representation of the day of the week |
||
40 | 'ddd' => 'D', // Textual representation of a day, three letters |
||
41 | 'dd' => 'd', // Day of the month, 2 digits with leading zeros |
||
42 | 'd' => 'j', // Day of the month without leading zeros |
||
43 | ], |
||
44 | self::KEY_HOUR_12 => [ |
||
45 | 'hh' => 'h', // 12-hour format of an hour without leading zeros |
||
46 | 'h' => 'g', // 12-hour format of an hour without leading zeros |
||
47 | ], |
||
48 | self::KEY_HOUR_24 => [ |
||
49 | 'hh' => 'H', // 24-hour hours with leading zero |
||
50 | 'h' => 'G', // 24-hour format of an hour without leading zeros |
||
51 | ], |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Converts the given Excel date format to a format understandable by the PHP date function. |
||
56 | * |
||
57 | * @param string $excelDateFormat Excel date format |
||
58 | * @return string PHP date format (as defined here: http://php.net/manual/en/function.date.php) |
||
59 | */ |
||
60 | 17 | public static function toPHPDateFormat($excelDateFormat) |
|
114 | |||
115 | /** |
||
116 | * @param string $excelDateFormat Date format as defined by Excel |
||
117 | * @return bool Whether the given date format has the 12-hour format marker |
||
118 | */ |
||
119 | 17 | private static function has12HourFormatMarker($excelDateFormat) |
|
123 | } |
||
124 |