I want to populate a select list such that it increments for each 5 minutes starting from 00:00 up til 23:55. I need to do this in UNIX time as well.
Below is what I’ve done but I’ve just realised that doing strtotime("18:00") doesn’t actually give me that hour specifically. I also have a select list of dates, for example a date can be “Tues 6 march 2012” (with the unix format being: 1330992000). I want to be able to take the two selected values and then add the two unix timestamps together so I can insert it into the database. For example, a user selects the time 17:00 which will give me 1332950400 (I know this is wrong) and then the date Tues 6 march 2012 which will give me 1330992000. I want to then be able to add the two timestamps together so I get 18:00 Tues 6 March 2012 in Unix timestamp format.
public function timeSelectList()
{
//using actual unix time instead of strtotime to save it from having to call the function each time.
$startTime = 1332889200; //strtotime('00:00');
$endTime = 1332975300; //strtotime('23:55');
$now = $startTime;
$startSelectList = '<label for="startSelect">Start Time</label><select name="startSelect" id="startSelect">';
$endSelectList = '<label for="endSelect">End Time</label><select name="endSelect" id="endSelect">';
//populates the select list with the starting date of the course up to the next six months
while($now <= $endTime)
{
if($now == 1332950400)// strtotime('17:00')
{
$startSelectList .= '<option value="'.$now.'" selected="selected">'.date('H:i', $now).'</option>';
$endSelectList .= '<option value="'.$now.'">'.date('H:i', $now).'</option>';
}
else if($now == 1332954000)//strtotime('18:00')
{
$startSelectList .= '<option value="'.$now.'">'.date('H:i', $now).'</option>';
$endSelectList .= '<option value="'.$now.'" selected="selected">'.date('H:i', $now).'</option>';
}
else
{
$startSelectList .= '<option value="'.$now.'">'.date('H:i', $now).'</option>';
$endSelectList .= '<option value="'.$now.'">'.date('H:i', $now).'</option>';
}
$now += 300; //increment 5 minutes (300 seconds = 5 minutes
}
$startSelectList .= '</select>';
$endSelectList .= '</select>';
return $startSelectList.$endSelectList;
}
It is a little unclear what the resulting form looks like. Nonetheless, in these situations it is easier to do string math than date math.
For example, add the date and time together as a string, then determine it’s timestamp.
Note: You’ll of course want to do some validation or string formatting beforehand. But the above should be the final piece you need.