From 56f54f02185461b2abcb6d5601022c8e374751ae Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 20 Oct 2020 14:54:27 +1300 Subject: [PATCH] Rationalise date formatting This came out of discovering a place in the code was 'accidentally formatted correctly' and finding it far too hard to figure out the right way to specify the date formatting - something that seems confirmed by finding various places with hard-coded date formattin rather than using the setting --- CRM/Contribute/Form/Contribution/Confirm.php | 4 ++-- CRM/Member/Form/Membership.php | 10 +++++----- CRM/Member/Form/MembershipRenewal.php | 4 ++-- CRM/Utils/Date.php | 13 +++++++++++++ tests/phpunit/CRM/Member/Form/MembershipTest.php | 4 ++-- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index d9642ae9bcf2..246b669576f7 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -1590,8 +1590,8 @@ protected function postProcessMembership( foreach ($form->_lineItem[$form->_priceSetId] as & $priceFieldOp) { if (!empty($priceFieldOp['membership_type_id']) && $membership->membership_type_id == $priceFieldOp['membership_type_id']) { $membershipOb = $membership; - $priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date::customFormat($membershipOb->start_date, '%B %E%f, %Y') : '-'; - $priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date::customFormat($membershipOb->end_date, '%B %E%f, %Y') : '-'; + $priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date::formatDateOnlyLong($membershipOb->start_date) : '-'; + $priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date::formatDateOnlyLong($membershipOb->end_date) : '-'; } else { $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A'; diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index b249c0818001..5b4500f15c9a 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -994,9 +994,9 @@ public static function emailReceipt(&$form, &$formValues, &$membership, $customV $form->assign('formValues', $formValues); if (empty($lineItem)) { - $form->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date, '%B %E%f, %Y')); + $form->assign('mem_start_date', CRM_Utils_Date::formatDateOnlyLong($membership->start_date)); if (!CRM_Utils_System::isNull($membership->end_date)) { - $form->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date, '%B %E%f, %Y')); + $form->assign('mem_end_date', CRM_Utils_Date::formatDateOnlyLong($membership->end_date)); } $form->assign('membership_name', CRM_Member_PseudoConstant::membershipType($membership->membership_type_id)); } @@ -1610,8 +1610,8 @@ public function submit() { $totalTaxAmount = 0; foreach ($lineItem[$this->_priceSetId] as & $priceFieldOp) { if (!empty($priceFieldOp['membership_type_id'])) { - $priceFieldOp['start_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'], '%B %E%f, %Y') : '-'; - $priceFieldOp['end_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'], '%B %E%f, %Y') : '-'; + $priceFieldOp['start_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'] ? CRM_Utils_Date::formatDateOnlyLong($membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date']) : '-'; + $priceFieldOp['end_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'] ? CRM_Utils_Date::formatDateOnlyLong($membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date']) : '-'; } else { $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A'; @@ -1845,7 +1845,7 @@ protected function getStatusMessageForCreate($endDate, $membershipTypes, $create } if ($memEndDate && $memEndDate !== 'null') { - $memEndDate = CRM_Utils_Date::customFormat($memEndDate); + $memEndDate = CRM_Utils_Date::formatDateOnlyLong($memEndDate); $statusMsg[$memType] .= ' ' . ts('The new membership End Date is %1.', [1 => $memEndDate]); } } diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index 33cdd9825592..87be03190375 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -682,8 +682,8 @@ protected function sendReceipt($membership) { $membership->membership_type_id )); $this->assign('customValues', $customValues); - $this->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date)); - $this->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date)); + $this->assign('mem_start_date', CRM_Utils_Date::formatDateOnlyLong($membership->start_date)); + $this->assign('mem_end_date', CRM_Utils_Date::formatDateOnlyLong($membership->end_date)); if ($this->_mode) { $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( $this->_params, diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 11008a84439b..af0fd42584f9 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -422,6 +422,19 @@ public static function customFormat($dateString, $format = NULL, $dateParts = NU } } + /** + * Format the field according to the site's preferred date format. + * + * This is likely to look something like December 31st, 2020. + * + * @param string $date + * + * @return string + */ + public static function formatDateOnlyLong(string $date):string { + return CRM_Utils_Date::customFormat($date, Civi::settings()->get('dateformatFull')); + } + /** * Wrapper for customFormat that takes a timestamp * diff --git a/tests/phpunit/CRM/Member/Form/MembershipTest.php b/tests/phpunit/CRM/Member/Form/MembershipTest.php index 91bb01be62f5..4c4de605d575 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -488,7 +488,7 @@ public function testSubmit($thousandSeparator) { $this->createLoggedInUser(); $params = [ 'cid' => $this->_individualId, - 'join_date' => date('2/d/Y'), + 'join_date' => date('Y-m-d'), 'start_date' => '', 'end_date' => '', // This format reflects the organisation & then the type. @@ -802,7 +802,7 @@ public function testSubmitRecur() { ], 1); $this->assertEquals([ [ - 'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been added. The new membership End Date is ' . date('F jS, Y', strtotime('last day of this month')) . ' 12:00 AM.', + 'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been added. The new membership End Date is ' . date('F jS, Y', strtotime('last day of this month')) . '.', 'title' => 'Complete', 'type' => 'success', 'options' => NULL,