devzone
devzone

Reputation: 305

How to create dropdown times listed change in php?

I want to have a dropdown times listed change. Times begin 1.5 hours after curent time. So, if the current time is 10:31 then the first available time is 12:30 pm-1:00pm

if the current time is 2:31pm the first available time is 2:30 pm-3:00pm.

here is the dropdown

12:30pm - 1:00pm 1:00pm - 1:30pm 1:30pm - 2:00pm 2:00pm - 3:30pm 2:30pm - 3:00pm 3:00pm - 3:30pm 3:30pm - 4:00pm 4:00pm - 4:30pm 5:00pm - 5:30pm 5:30pm - 6:00pm

Any help? Thanks! Help much appreciated!

Upvotes: 1

Views: 1485

Answers (2)

jon_darkstar
jon_darkstar

Reputation: 16768

Use time and date functions, round to half hours.

$timeAtHalfHour = time() - ( time() % (60*30));
//$timeAtHalfHour += 90*60;  //IDK IF YOU WANT THIS?
$endTime = strtotime('6:00 pm');
$numIntervals = ($endTime - $timeAtHalfHour) / (60*30);
echo "<select name='timeInterval'>";
$strTime1 = date('h:iA', $timeAtHalfHour);
foreach(range(1,$numIntervals) as $cur)
{
   $thisTime = $timeAtHalfHour + 30*60*$cur;
   $strTime2 = date('h:iA', $thisTime);
   echo "<option value=$cur>$strTime1 - $strTime2 </option><br />";
   $strTime1 = $strTime2;
}
echo "</select>";

EDIT

$timeAtHalfHour = time() - ( time() % (60*30));
//$timeAtHalfHour += 90*60;  //IDK IF YOU WANT THIS?
$endTime = strtotime('6:00 pm');
$numIntervals = ($endTime - $timeAtHalfHour) / (60*30);
$strTime1a = date('h:iA', $timeAtHalfHour); $strTime2a = date('h:iA', $timeAtHalfHour + 4*60*30);
$optionStr1 = ''; $optionStr2 = '';
foreach(range(1,$numIntervals) as $cur)
{
   $thisTime1 = $timeAtHalfHour + 30*60*$cur; $thisTime2 = $thisTime1 + (60*30*4);
   $strTime1b = date('h:iA', $thisTime1); $strTime2b = date('h:iA', $thisTime2);
   $optionStr1 .= "<option value=$cur>$strTime1a - $strTime1b </option><br />";
   $optionStr2 .= "<option value=$cur>$strTime2a - $strTime2b </option><br />";
   $strTime1a = $strTime1b; $strTime2a = $strTime2b;
}
echo "<select name='firstTime'>$optionStr1</select>";
echo "<select name='secondTime'>$optionStr2</select>";

Its just pretty much making a second time parallel to the first time, just 4 half hour intervals ahead.

Upvotes: 2

Robert van der Linde
Robert van der Linde

Reputation: 346

What you can do is: - For each timeperiod listed in the dropdown, calculate the unix timestamp (strtotime()) of the start time. Then you calculate the current timestamp (time()). Then remove all timeperiods where the timestamp of the start time is less then 90*60 seconds from the current time: if($curTime+(90*60) > $startTimestampOfPeriod){}

Upvotes: 0

Related Questions