Early booking reservation system using DateTime

I want to offer a discount based on early booking but not sure how to get the discount that applies based on the current date.
I have seen some usort examples but not very familiar to me.
I want to keep using DateTime objects (not timestamps).

[php]
$today = new DateTime();
$data[‘earlybooking’] = array(
array(‘percentage’ => ‘25’, ‘beforedate’ => ‘10-12-2016’ ),
array(‘percentage’ => ‘15’, ‘beforedate’ => ‘31-03-2017’ ),
array(‘percentage’ => ‘10’, ‘beforedate’ => ‘30-04-2017’ ),
array(‘percentage’ => ‘20’, ‘beforedate’ => ‘31-01-2017’ )
);

// foreach ? Or maybe usort?
// usort example: http://stackoverflow.com/questions/7127764/sort-array-of-objects-by-date-field
[/php]

Sorting makes no difference, so not sure why you are pursuing that aspect.

[php]function get_percentage()
{
$today = new DateTime();
$earlybooking = array(
array(
‘percentage’ => ‘25’,
‘beforedate’ => ‘10-12-2016’
),
array(
‘percentage’ => ‘20’,
‘beforedate’ => ‘31-01-2017’
),
array(
‘percentage’ => ‘15’,
‘beforedate’ => ‘31-03-2017’
),
array(
‘percentage’ => ‘10’,
‘beforedate’ => ‘30-04-2017’
),
);
foreach ($earlybooking as $key => $val) {
if ( new DateTime($val[‘beforedate’]) >= $today)
return $val[‘percentage’];
}
return false;
}[/php]

Not sure your code would be anough

You are correct the information I need is the percentage.
However, the sorting might be needed because the time of booking (today) might meet the criteria of more than one of the “beforedate” dates.

So for example, 03-03-2017 would meet the condition you put in your “if” twice, for both beforedate 31-03-2017 and beforedate 30-04-2017

You do have me there. Being that these are hardcoded values and not being pulled from another storage media, I would just do the reordering by hand as well.

Hence the need for sorting.
If the array is sorted by date, your code should be modified to exit on first condition met:

[php]
foreach ($earlybooking as $key => $val) {
if ( new DateTime($val[‘beforedate’]) >= $today)
return $val[‘percentage’];
break;
}
[/php]

It does that. Return, returns back to the caller. Your break statement means only one iteration of the loop would occur.

Since the array is not sorted, your code would return even if the condition met is not the correct one (since there can be more than one).

You are correct about my code being wrong as well, sorry about that, I was in a hurry, but yours definitely won;t do the job either.

If you presort the array, which I mentioned, because it is hard coded (which means it doesn’t need sorting, you just have to hardcode the values in a sorted order) it will return the first time it reaches a useable date.

Sorting programmatically is more usful when you don’t know the order it is coming in as. You hardcoded it, so you should know what the order is.

What do you think of my solution that might not need sorting at all:

[php]
function get_earlybooking_percentage($earlybooking) {
$today = new DateTime();
$matching = array();

foreach ($earlybooking as $item) {
if ($today < DateTime::createFromFormat('d-m-Y', $item['beforedate'])) {
        $matching[] = (int) $item['percentage'];
    }
}
return max($matching);

}

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service