| 1 |
<?php |
| 2 |
|
| 3 |
namespace Bookit\Helpers; |
| 4 |
|
| 5 |
use Bookit\Classes\Admin\SettingsController; |
| 6 |
|
| 7 |
/** |
| 8 |
* Bookit Time Slot Helper |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
class TimeSlotHelper { |
| 13 |
|
| 14 |
/** slot length **/ |
| 15 |
const TIME_SLOT_POSSIBLE_VALUES = [ |
| 16 |
'15_minutes' => 15, |
| 17 |
'20_minutes' => 20, |
| 18 |
'30_minutes' => 30, |
| 19 |
'45_minutes' => 45, |
| 20 |
'1_hour' => 60 |
| 21 |
]; |
| 22 |
const DAY_IN_SECONDS = 60 * 60 * 24; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param integer $start - time in seconds |
| 26 |
* @param integer $end - time in seconds |
| 27 |
* @param string $type - possible values "default|admin" |
| 28 |
*/ |
| 29 |
public static function getTimeList($start, $end) { |
| 30 |
$result = []; |
| 31 |
$time_slot_duration = get_option_by_path('bookit_settings.time_slot_duration') ?: SettingsController::$time_slot_default_duration; |
| 32 |
|
| 33 |
while ( $start <= $end ) { |
| 34 |
$slot = [ |
| 35 |
'value' => self::timeFromSeconds($start), |
| 36 |
'label' => self::timeFormat( $start ), |
| 37 |
]; |
| 38 |
$result[] = $slot; |
| 39 |
$start += ( (int)$time_slot_duration * 60 ); |
| 40 |
} |
| 41 |
|
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
public static function timeFormat($time) { |
| 46 |
return date_i18n( get_option( 'time_format' ), is_numeric( $time ) ? $time : strtotime( $time, current_time( 'timestamp' ) ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Build time string from seconds |
| 51 |
* |
| 52 |
* @param int $seconds |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
public static function timeFromSeconds( $seconds ) { |
| 56 |
$seconds = abs( $seconds ); |
| 57 |
$hours = (int) ( $seconds / 3600 ); |
| 58 |
$seconds -= $hours * 3600; |
| 59 |
$minutes = (int) ( $seconds / 60 ); |
| 60 |
$seconds -= $minutes * 60; |
| 61 |
|
| 62 |
return sprintf( '%s%02d:%02d:%02d', $seconds < 0 ? '-' : '', $hours, $minutes, $seconds ); |
| 63 |
} |
| 64 |
|
| 65 |
} |