The following code will calculate a recurring fortnightly date from a given date, i.e. a star-date using PHP. The date format should be ‘Y-m-d’ for the following code. However, you can make updates in it as per requirements.
For example: The start date for the fortnight cycle date was set to be as “2018-01-10”. Then, as per current cycle, next will be Start Date: 2018-01-10 | Next Date: 2023-05-23.
<?php /* Fortnight Calculation in JavaScript */ function getFortNightString(sDate) { var param = []; var objDate = new Date(); param.todayDate = objDate.getFullYear() + '-' + ("0" + (objDate.getMonth() + 1)).slice(-2) + '-' + ("0" + (objDate.getDate())).slice(-2); param.todayDate = new Date(param.todayDate).getTime(); param.sDate = new Date(sDate).getTime(); param.timeDiff = Math.abs(param.todayDate - param.sDate); param.diffDays = Math.ceil(param.timeDiff / (1000 * 3600 * 24)); if (param.diffDays > 0 && param.sDate > param.todayDate) {
param.diffDays = (14 + (param.diffDays % 14));
} else {
param.diffDays = (14 - (param.diffDays % 14));
}
param.dayCount = param.diffDays - 1;
objDate.setDate(objDate.getDate() + param.dayCount);
objDate = objDate.getFullYear() + '-' + ("0" + (objDate.getMonth() + 1)).slice(-2) + '-' + ("0" + (objDate.getDate())).slice(-2);
return " Start Date: " + sDate + " | Next Date: " + objDate;
}
/* Fortnight Calculation in PHP */
$sDate = "2018-02-05";
$sDate = date_create($sDate);
$today = date_create(date("Y-m-d")); // or your date as well
$dateDiff = date_diff($sDate, $today)->format("%a");
if($dateDiff > 0 && $sDate > $today){
$dateDiff = (14 + ($dateDiff % 14));
}else{
$dateDiff = (14 - ($dateDiff % 14));
}
$dateDiff--;
echo date('Y-m-d', strtotime("$dateDiff day", strtotime(date('Y-m-d'))));