Skip to content

Instantly share code, notes, and snippets.

@bor0
Last active April 5, 2020 20:22
Show Gist options
  • Save bor0/4154eca676efe1b7192ae8804df7b89d to your computer and use it in GitHub Desktop.
Save bor0/4154eca676efe1b7192ae8804df7b89d to your computer and use it in GitHub Desktop.
Test rlanvin/php-rrule events
<?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' ) );
@bor0
Copy link
Author

bor0 commented Apr 5, 2020

$events is a list of booked events. Availability logic array can be built in a similar way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment