Early booking reservation system

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
bg17aw
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Mar 03, 2017 5:27 am

Fri Mar 03, 2017 5:53 am

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

Code: Select all

$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

Any ideas? Thanks

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Fri Mar 03, 2017 7:41 am

I'm not sure if your solution really captures the requirement.
I would have thought that the array 'earlybooking' would be more based on the number of days rather than a date. So (for example) if you book 10 days before you get 10%, 20 days would be 15% etc.
This would mean that you could look at the date of departure (or whatever it is) and the date booked and then find the appropriate number of days ( using 'diff' in the datetime class and finding the 'days' element). The find the element in the earlybooking array which covers that number of days.

bg17aw
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Mar 03, 2017 5:27 am

Fri Mar 03, 2017 9:12 am

That's probably another approach, but it is easier for a tourist to calculate " I get 10% if I book before 30-04-2017" than "I get 10% if I book 36 days before a certain date, oh carp how many days till then?"

All I need is a solution to select the correct percentage based on the today date, which is the booking date, so if today is before 30-04-2017, the percentage is 10%

I know it is dead simple to compare dates with < and > but not sure how to handle the cases where today would meet more than one condition, and that is why I was thinking that maybe I should sort the array first so that the first time the condition is met I can be sure it is what I want. Not sure if it is the best approach though

Post Reply