License
6 years ago
Notices
6 years ago
pQuery
6 years ago
ConflictResolver.php
6 years ago
Cookies.php
6 years ago
DOM.php
6 years ago
DateConverter.php
6 years ago
FreeDomains.php
6 years ago
Helpers.php
6 years ago
Installation.php
6 years ago
ProgressBar.php
6 years ago
SecondLevelDomainNames.php
6 years ago
Security.php
6 years ago
Url.php
6 years ago
index.php
9 years ago
DateConverter.php
95 lines
| 1 | <?php |
| 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 bool|string |
| 13 | */ |
| 14 | public function convertDateToDatetime(string $date, string $dateFormat) { |
| 15 | $datetime = false; |
| 16 | if ($dateFormat === 'datetime') { |
| 17 | $datetime = $date; |
| 18 | } else { |
| 19 | $parsedDate = explode('/', $date); |
| 20 | $parsedDateFormat = explode('/', $dateFormat); |
| 21 | $yearPosition = array_search('YYYY', $parsedDateFormat); |
| 22 | $monthPosition = array_search('MM', $parsedDateFormat); |
| 23 | $dayPosition = array_search('DD', $parsedDateFormat); |
| 24 | if (count($parsedDate) === 3) { |
| 25 | // create date from any combination of month, day and year |
| 26 | $parsedDate = [ |
| 27 | 'year' => $parsedDate[$yearPosition], |
| 28 | 'month' => $parsedDate[$monthPosition], |
| 29 | 'day' => $parsedDate[$dayPosition], |
| 30 | ]; |
| 31 | } else if (count($parsedDate) === 2) { |
| 32 | // create date from any combination of month and year |
| 33 | $parsedDate = [ |
| 34 | 'year' => $parsedDate[$yearPosition], |
| 35 | 'month' => $parsedDate[$monthPosition], |
| 36 | 'day' => '01', |
| 37 | ]; |
| 38 | } else if ($dateFormat === 'MM' && count($parsedDate) === 1) { |
| 39 | // create date from month |
| 40 | if ((int)$parsedDate[$monthPosition] === 0) { |
| 41 | $datetime = ''; |
| 42 | $parsedDate = false; |
| 43 | } else { |
| 44 | $parsedDate = [ |
| 45 | 'month' => $parsedDate[$monthPosition], |
| 46 | 'day' => '01', |
| 47 | 'year' => date('Y'), |
| 48 | ]; |
| 49 | } |
| 50 | } else if ($dateFormat === 'YYYY' && count($parsedDate) === 1) { |
| 51 | // create date from year |
| 52 | if ((int)$parsedDate[$yearPosition] === 0) { |
| 53 | $datetime = ''; |
| 54 | $parsedDate = false; |
| 55 | } else { |
| 56 | $parsedDate = [ |
| 57 | 'year' => $parsedDate[$yearPosition], |
| 58 | 'month' => '01', |
| 59 | 'day' => '01', |
| 60 | ]; |
| 61 | } |
| 62 | } else { |
| 63 | $parsedDate = false; |
| 64 | } |
| 65 | if ($parsedDate) { |
| 66 | $year = $parsedDate['year']; |
| 67 | $month = $parsedDate['month']; |
| 68 | $day = $parsedDate['day']; |
| 69 | // if all date parts are set to 0, date value is empty |
| 70 | if ((int)$year === 0 && (int)$month === 0 && (int)$day === 0) { |
| 71 | $datetime = ''; |
| 72 | } else { |
| 73 | if ((int)$year === 0) $year = date('Y'); |
| 74 | if ((int)$month === 0) $month = date('m'); |
| 75 | if ((int)$day === 0) $day = date('d'); |
| 76 | $datetime = sprintf( |
| 77 | '%s-%s-%s 00:00:00', |
| 78 | $year, |
| 79 | $month, |
| 80 | $day |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | if ($datetime !== false && !empty($datetime)) { |
| 86 | try { |
| 87 | $datetime = Carbon::parse($datetime)->toDateTimeString(); |
| 88 | } catch (\Exception $e) { |
| 89 | $datetime = false; |
| 90 | } |
| 91 | } |
| 92 | return $datetime; |
| 93 | } |
| 94 | } |
| 95 |