Last active
April 5, 2020 20:22
-
-
Save bor0/4154eca676efe1b7192ae8804df7b89d to your computer and use it in GitHub Desktop.
Test rlanvin/php-rrule events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use RRule\RRule; | |
use RRule\Proxy; | |
use RRule\Event; | |
use RRule\ProxyIterator; | |
require_once( 'vendor/autoload.php' ); | |
$events = array( | |
[ | |
'FREQ' => 'HOURLY', | |
'INTERVAL' => 1, | |
'DTSTART' => '2019-10-01 00:00', | |
'UNTIL' => '2019-10-01 00:59', | |
], | |
[ | |
'FREQ' => 'HOURLY', | |
'INTERVAL' => 1, | |
'DTSTART' => '2019-10-01 01:00', | |
'UNTIL' => '2019-10-01 01:59', | |
], | |
); | |
function is_bookable( $event_rrules, $date_start, $date_end ) { | |
$date_start = new DateTime( $date_start ); | |
$date_end = new DateTime( $date_end ); | |
$event_rrules = array_map( function( $rrule ) { | |
return new Proxy( | |
new RRule( $rrule ), | |
function ( \DateTimeInterface $occurrence ) use ( $rrule ) { | |
$duration = abs( strtotime( $rrule['DTSTART'] ) - strtotime( $rrule['UNTIL'] ) ); | |
return new \RRule\Event( $occurrence, $duration ); | |
} | |
); | |
}, $event_rrules ); | |
foreach ( $event_rrules as $rrules ) { | |
foreach ( $rrules as $rrule ) { | |
if ( $rrule->getStart() <= $date_end && $date_start <= $rrule->getEnd() ) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
// Blocked by event 1 | |
var_dump( is_bookable( $events, '2019-10-01 00:00:00', '2019-10-01 00:59:00' ) ); | |
// Blocked by event 2 | |
var_dump( is_bookable( $events, '2019-10-01 01:00:00', '2019-10-01 01:59:00' ) ); | |
// Available | |
var_dump( is_bookable( $events, '2019-10-01 02:00:00', '2019-10-01 02:59:00' ) ); | |
// Blocked by event 1 | |
var_dump( is_bookable( $events, '2019-10-01 00:30:00', '2019-10-01 00:39:00' ) ); | |
// Blocked by event 2 | |
var_dump( is_bookable( $events, '2019-10-01 01:30:00', '2019-10-01 02:00:00' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$events
is a list of booked events. Availability logic array can be built in a similar way.