Wdblazer
12-11-2010, 06:42 AM
I have an select option in php that show the default as the selected time from database. The time format from MYSQL is HH:MM:SS, how do I separate them into each variable, $eventTimeHour = HH, $eventTimeMin =MM?
<?php
//Mysql code for connection and retrieving the result here;
// $eventTime is given as HH:MM:00
//how to parse them as $eventTimeHour = HH and $eventTimeMin = MM?
$eventTime = $row['TIME'];
for ($hr = 1; $hr < 13; $hr++)
{
$hr = str_pad($hr, 2, '0', STR_PAD_LEFT);
if ($hr == $eventTimeHour)
{
echo "<option selected>$hr</option>";
}
else
{
echo "<option>$hr</option>";
}
}
</select>
<select name="EventTimeMin" id="EventTimeMin">
<?php
for ($min = 00; $min < 60; $min++)
{
$min = str_pad($min, 2, '0', STR_PAD_LEFT);
if ($min == $eventTimeMin)
{
echo "<option selected>$min</option>";
}
else
{
echo "<option>$min</option>";
}
}
?>
edit: silly me, I remember the substr function and is using it to get the first 2 and 4th and 5th digits.
<?php
//Mysql code for connection and retrieving the result here;
// $eventTime is given as HH:MM:00
//how to parse them as $eventTimeHour = HH and $eventTimeMin = MM?
$eventTime = $row['TIME'];
for ($hr = 1; $hr < 13; $hr++)
{
$hr = str_pad($hr, 2, '0', STR_PAD_LEFT);
if ($hr == $eventTimeHour)
{
echo "<option selected>$hr</option>";
}
else
{
echo "<option>$hr</option>";
}
}
</select>
<select name="EventTimeMin" id="EventTimeMin">
<?php
for ($min = 00; $min < 60; $min++)
{
$min = str_pad($min, 2, '0', STR_PAD_LEFT);
if ($min == $eventTimeMin)
{
echo "<option selected>$min</option>";
}
else
{
echo "<option>$min</option>";
}
}
?>
edit: silly me, I remember the substr function and is using it to get the first 2 and 4th and 5th digits.