Skip to content

Instantly share code, notes, and snippets.

@matthewpoer
Created April 17, 2019 18:36
Show Gist options
  • Select an option

  • Save matthewpoer/69ab9b30f96daa4c33bd97b83aa7be25 to your computer and use it in GitHub Desktop.

Select an option

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.
<?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