Created
January 4, 2018 13:06
-
-
Save OkoyaUsman/e5fbab5456dd2f966ffc7500e3918d39 to your computer and use it in GitHub Desktop.
Create time range function in php
This file contains hidden or 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 | |
// Below is a php time range function you can add in your php library and call it where you want to add time range dropdown. | |
function createTimeRange($startTime, $endTime, $interval = '15 mins', $format = '12') { | |
try { | |
if(!isset($startTime) || !isset($endTime)) { | |
throw new exception("Start or End time is missing"); | |
} | |
$startTime = strtotime($startTime); | |
$endTime = strtotime($endTime); | |
$currentTime = time(); | |
$addTime = strtotime('+'.$interval, $currentTime); | |
$timeDiff = $addTime - $currentTime; | |
$timesArr = array(); | |
$timeFormat = ($format == '12')?'g:i:s A':'G:i:s'; | |
while ($startTime < $endTime) { | |
$timesArr[] = date($timeFormat, $startTime); | |
$startTime += $timeDiff; | |
} | |
return $timesArr; | |
} catch (Exception $e) { | |
return $e->getMessage(); | |
} | |
} | |
// In the above function you have to pass two mandatory parameter Start and End time and can also pass two optional parameter. | |
/* | |
$startTime » Required. Specifies the start time. E.g., 7:30am or 7:30 | |
$endTime » Required. Specifies the end time. E.g., 8:15pm or 20:30 | |
$interval » Optional. Specifies the interval used in time range. E.g., 1 hour, 1 mins, 1 secs, etc | |
$format » Optional. Specifies which time format you want to get in the time range. E.g., 12 or 24 | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment