PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 5.34.1
MailPoet – Newsletters, Email Marketing, and Automation v5.34.1
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / Util / DateConverter.php
DateConverter.php
110 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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