License
2 years ago
Notices
1 year ago
pQuery
3 years ago
APIPermissionHelper.php
3 years ago
CdnAssetUrl.php
3 years ago
ConflictResolver.php
2 years ago
Cookies.php
2 years ago
DBCollationChecker.php
4 years ago
DOM.php
2 years ago
DateConverter.php
3 years ago
FreeDomains.php
3 years ago
Helpers.php
2 years ago
Installation.php
3 years ago
ProgressBar.php
2 years ago
Request.php
3 years ago
SecondLevelDomainNames.php
3 years ago
Security.php
3 years ago
Url.php
3 years ago
index.php
3 years ago
DateConverter.php
110 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Util; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoetVendor\Carbon\Carbon; |
| 9 | |
| 10 | class DateConverter { |
| 11 | /** |
| 12 | * @return string|false |
| 13 | */ |
| 14 | public function convertDateToDatetime(string $date, string $dateFormat) { |
| 15 | $datetime = false; |
| 16 | |
| 17 | if ($dateFormat === 'datetime') { |
| 18 | $datetime = $date; |
| 19 | } elseif ($dateFormat === 'd/m/Y') { |
| 20 | $datetime = str_replace('/', '-', $date); |
| 21 | } else { |
| 22 | $parsedDate = explode('/', $date); |
| 23 | $parsedDateFormat = explode('/', $dateFormat); |
| 24 | $yearPosition = array_search('YYYY', $parsedDateFormat); |
| 25 | $monthPosition = array_search('MM', $parsedDateFormat); |
| 26 | $dayPosition = array_search('DD', $parsedDateFormat); |
| 27 | if (count($parsedDate) === 3) { |
| 28 | // create date from any combination of month, day and year |
| 29 | $parsedDate = [ |
| 30 | 'year' => $parsedDate[$yearPosition], |
| 31 | 'month' => $parsedDate[$monthPosition], |
| 32 | 'day' => $parsedDate[$dayPosition], |
| 33 | ]; |
| 34 | } else if (count($parsedDate) === 2) { |
| 35 | // create date from any combination of month and year |
| 36 | $parsedDate = [ |
| 37 | 'year' => $parsedDate[$yearPosition], |
| 38 | 'month' => $parsedDate[$monthPosition], |
| 39 | 'day' => '01', |
| 40 | ]; |
| 41 | } else if ($dateFormat === 'MM' && count($parsedDate) === 1) { |
| 42 | // create date from month |
| 43 | if ((int)$parsedDate[$monthPosition] === 0) { |
| 44 | $datetime = ''; |
| 45 | $parsedDate = false; |
| 46 | } else { |
| 47 | $parsedDate = [ |
| 48 | 'month' => $parsedDate[$monthPosition], |
| 49 | 'day' => '01', |
| 50 | 'year' => date('Y'), |
| 51 | ]; |
| 52 | } |
| 53 | } else if ($dateFormat === 'YYYY' && count($parsedDate) === 1) { |
| 54 | // create date from year |
| 55 | if ((int)$parsedDate[$yearPosition] === 0) { |
| 56 | $datetime = ''; |
| 57 | $parsedDate = false; |
| 58 | } else { |
| 59 | $parsedDate = [ |
| 60 | 'year' => $parsedDate[$yearPosition], |
| 61 | 'month' => '01', |
| 62 | 'day' => '01', |
| 63 | ]; |
| 64 | } |
| 65 | } else if ($dateFormat === 'DD' && count($parsedDate) === 1) { |
| 66 | // create date from day |
| 67 | if ((int)$parsedDate[$dayPosition] === 0) { |
| 68 | $datetime = ''; |
| 69 | $parsedDate = false; |
| 70 | } else { |
| 71 | $parsedDate = [ |
| 72 | 'year' => date('Y'), |
| 73 | 'month' => '01', |
| 74 | 'day' => $parsedDate[$dayPosition], |
| 75 | ]; |
| 76 | } |
| 77 | } else { |
| 78 | $parsedDate = false; |
| 79 | } |
| 80 | if ($parsedDate) { |
| 81 | $year = $parsedDate['year']; |
| 82 | $month = $parsedDate['month']; |
| 83 | $day = $parsedDate['day']; |
| 84 | // if all date parts are set to 0, date value is empty |
| 85 | if ((int)$year === 0 && (int)$month === 0 && (int)$day === 0) { |
| 86 | $datetime = ''; |
| 87 | } else { |
| 88 | if ((int)$year === 0) $year = date('Y'); |
| 89 | if ((int)$month === 0) $month = date('m'); |
| 90 | if ((int)$day === 0) $day = date('d'); |
| 91 | $datetime = sprintf( |
| 92 | '%s-%s-%s 00:00:00', |
| 93 | $year, |
| 94 | $month, |
| 95 | $day |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | if ($datetime !== false && !empty($datetime)) { |
| 101 | try { |
| 102 | $datetime = Carbon::parse($datetime)->toDateTimeString(); |
| 103 | } catch (\Exception $e) { |
| 104 | $datetime = false; |
| 105 | } |
| 106 | } |
| 107 | return $datetime; |
| 108 | } |
| 109 | } |
| 110 |