diff --git a/lib/API/Endpoints/v1/Services.php b/lib/API/Endpoints/v1/Services.php index 996248ccf4..b8e3486a53 100644 --- a/lib/API/Endpoints/v1/Services.php +++ b/lib/API/Endpoints/v1/Services.php @@ -1,7 +1,6 @@ format($this->date_time->getDateFormat()) + $this->date_time->formatDate(strtotime($result['data']['expire_at'])) ); } @@ -94,8 +92,7 @@ class Services extends APIEndpoint { } elseif($state == Bridge::PREMIUM_KEY_EXPIRING) { $success_message = sprintf( __('Your license key expires on %s.', 'mailpoet'), - Carbon::createFromTimestamp(strtotime($result['data']['expire_at'])) - ->format($this->date_time->getDateFormat()) + $this->date_time->formatDate(strtotime($result['data']['expire_at'])) ); } diff --git a/lib/Config/ServicesChecker.php b/lib/Config/ServicesChecker.php index e139299cc2..58e9b2bcbd 100644 --- a/lib/Config/ServicesChecker.php +++ b/lib/Config/ServicesChecker.php @@ -36,7 +36,7 @@ class ServicesChecker { ) { if($display_error_notice) { $date_time = new DateTime(); - $date = date($date_time->getDateFormat(), strtotime($mss_key['data']['expire_at'])); + $date = $date_time->formatDate(strtotime($mss_key['data']['expire_at'])); $error = Helpers::replaceLinkTags( __('Your newsletters are awesome! Don\'t forget to [link]upgrade your MailPoet email plan[/link] by %s to keep sending them to your subscribers.', 'mailpoet'), 'https://account.mailpoet.com?s=' . Subscriber::getTotalSubscribers() @@ -77,7 +77,7 @@ class ServicesChecker { ) { if($display_error_notice) { $date_time = new DateTime(); - $date = date($date_time->getDateFormat(), strtotime($premium_key['data']['expire_at'])); + $date = $date_time->formatDate(strtotime($premium_key['data']['expire_at'])); $error = Helpers::replaceLinkTags( __('Your License Key is expiring! Don\'t forget to [link]renew your license[/link] by %s to keep enjoying automatic updates and Premium support.', 'mailpoet'), 'https://account.mailpoet.com' diff --git a/lib/WP/DateTime.php b/lib/WP/DateTime.php index eec72efa2c..43bc305dc2 100644 --- a/lib/WP/DateTime.php +++ b/lib/WP/DateTime.php @@ -38,6 +38,12 @@ class DateTime { return date($format, $timestamp); } + function formatDate($timestamp, $format=false) { + if (empty($format)) $format = $this->getDateFormat(); + + return date($format, $timestamp); + } + /** * Generates a list of time strings within an interval, * formatted and mapped from DEFAULT_TIME_FORMAT to WordPress time strings. diff --git a/tests/unit/WP/DateTimeTest.php b/tests/unit/WP/DateTimeTest.php index f247eae870..11ce366314 100644 --- a/tests/unit/WP/DateTimeTest.php +++ b/tests/unit/WP/DateTimeTest.php @@ -42,9 +42,17 @@ class DateTimeTest extends MailPoetTest { function testFormatTime() { $timestamp = 1234567; $format = "H:i:s"; + expect($this->date_time->formatTime($timestamp))->equals(date($this->date_time->getTimeFormat(), $timestamp)); expect($this->date_time->formatTime($timestamp, $format))->equals(date($format, $timestamp)); } + function testFormatDate() { + $timestamp = 1234567; + $format = "Y-m-d"; + expect($this->date_time->formatDate($timestamp))->equals(date($this->date_time->getDateFormat(), $timestamp)); + expect($this->date_time->formatDate($timestamp, $format))->equals(date($format, $timestamp)); + } + function testTimeInterval() { $one_hour_interval = array_keys($this->date_time->getTimeInterval( '00:00:00',