Created
April 17, 2019 18:36
-
-
Save matthewpoer/69ab9b30f96daa4c33bd97b83aa7be25 to your computer and use it in GitHub Desktop.
Given a date string, find 1 year out from that date, rounded up or down to the month. Based on a membership renewal cycle requirement.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function get_date($input) { | |
| $dateObject = new DateTime($input); | |
| if($dateObject->format('d') > 15) $dateObject->modify('+1 month'); | |
| $dateObject->modify('first day of'); // set to start of month | |
| $Start_Date__c = $dateObject->format('Y-m-d'); | |
| $dateObject->modify('+1 year'); | |
| $dateObject->modify('-1 day'); | |
| $End_Date__c = $dateObject->format('Y-m-d'); | |
| return array($Start_Date__c, $End_Date__c); | |
| } | |
| $date_test = array( | |
| '2019-01-01' => array('2019-01-01','2019-12-31'), | |
| '2019-01-14' => array('2019-01-01','2019-12-31'), | |
| '2019-01-15' => array('2019-01-01','2019-12-31'), | |
| '2019-01-16' => array('2019-02-01','2020-01-31'), | |
| '2019-06-16' => array('2019-07-01','2020-06-30'), | |
| '2019-10-05' => array('2019-10-01','2020-09-30'), | |
| '2019-10-17' => array('2019-11-01','2020-10-31'), | |
| '2019-11-10' => array('2019-11-01','2020-10-31'), | |
| '2019-11-20' => array('2019-12-01','2020-11-30'), | |
| '2019-12-08' => array('2019-12-01','2020-11-30'), | |
| '2019-12-16' => array('2020-01-01','2020-12-31'), | |
| ); | |
| foreach($date_test as $input => $output) { | |
| $result = get_date($input); | |
| if($result[0] != $output[0] || $result[1] != $output[1]) { | |
| echo 'Failed for ' . $input . PHP_EOL; | |
| echo ' expected ' . print_r($output, true) . ' received ' . print_r($result, true) . PHP_EOL; | |
| die(); | |
| } | |
| else { | |
| echo 'Passed for ' . $input . PHP_EOL; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment