Johnny
Johnny

Reputation: 319

PHP Dates getting a start date and end date for each period

Okay I'm trying to generate the end of the billing period from one date; my question is if I have a variable $billing_period = 02/28/2016 and the billing periods are on every 14th and 28th of each month. from this one variable how can I generate the end date of the period when they have different days appart depending on the start date?

What's confusing to me is that if the date is the 28th it has either 15 or 16days apart from the 14th which is the start of the next billing period. And if the date is the 14th then it has 14 days apart from the 28th. Thanks for any help

EDIT - The image here shows the date which is selected which is the 02/14/2016 how can I echo the next billing date which would be 02/28/2016 from just the start date

This is my code for the array and getting the start date.

<?
    $date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28',
'16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
'16-11-14','16-11-28','16-12-14','16-12-28');
    $currentdate = date('y-m-d');
 foreach ($date as $i => $d) {
    if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
        $selected = "selected";
        $selected_int = $i;
    } else {
        $selected = "";
    }
    list($year, $month, $day) = explode('-', $d);
    echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
 }
    ?>

Upvotes: 1

Views: 1694

Answers (3)

jpganz18
jpganz18

Reputation: 5858

Hope I understood well.

What you are trying to do is determine which is your billing period depending on the date you previously selected.

So, you select a date ($date).

Then you need to know the selected day

$timestamp = strtotime($date);
$day = date('D', $timestamp);

Now that you have your day you can make the comparations.

$datetime = new DateTime($date);
if ($day == 14 ){ // if you select 14th then your billing is on the 28th 
    $billing = $datetime->modify($date->format('Y-m-28'));
}else{ // if you didn't select 14th, then you select 28, and you add one month and set the day in 14th.
    $next_month = $datetime->modify('+1 month');
    $billing = $next_month->modify($date->format('Y-m-14'));
}

Upvotes: 1

Mikey
Mikey

Reputation: 2704

Here the code to get you the next date, just in case you want to do it as a one off.

$date = new DateTime();

$next = $date->format('14/m/Y');
if ($date->format('d') >= 14 && $date->format('d') < 28) {
    $next = $date->format('28/m/Y');
}

if ($date->format('d') >= 28) {
    $date->modify('+1 month');
    $next = $date->format('14/m/Y');
}

echo $next;

Here's the code turned into the function which you can call whenever you need it, it'll return a DateTime() object so you can output the date in whatever format you need at the time, or perform further comparisons/modifications should you need to down the line.

function nextBillingDate($date, $format = 'd/m/Y') {
    $date = DateTime::createFromFormat($format, $date);
    $next = $date->format('14/m/Y');
    if ($date->format('d') >= 14 && $date->format('d') <= 28) {
        $next = $date->format('28/m/Y');
    }

    if ($date->format('d') >= 28) {
        $date->modify('+1 month');
        $next = $date->format('14/m/Y');
    }

    return DateTime::createFromFormat('d/m/Y', $next);
}

Example use:

$date = nextBillingDate('28/02/2016');
echo $date->format('d/m/Y');

Outputs:

14/03/2016

Hope it helps.

Upvotes: 0

dex
dex

Reputation: 137

Build an array with the billing dates, get the current key and increment it to get the next value. Simple example with strings:

$dates    = ['02/28/2016', '03/14/2016', '03/28/2016', ...];
$key      = array_search( '03/14/2016', $dates );
$nextDate = $dates[$key + 1]; // 03/28/2016

Is this what you want to get?

Upvotes: 0

Related Questions