I was wondering if I could do some performance tuning for the below PHP Script. What I am trying to do - is get every half hour from 00:00am to 23:30pm into a select list. Here is the code
<?php
$starttime = '00:00';
$time = new DateTime($starttime);
$interval = new DateInterval('PT30M');
$temptime = $time->format('H:i');
do{
echo '<option value="">'.date("H:i a", strtotime($temptime)).'</option>';
$time->add($interval);
$temptime = $time->format('H:i');
}while ($temptime !== $starttime);
?>
I believe there is a way we can do it in an easier manner - but I cannot think of it. Can someone help?
Other than hardcoding the values, you can lose all the DateTime and TimeInterval objects and use a simple loop:
for ($h=0;$h<24;$h++) {
echo '<option>' + sprintf ( "%02d", $h ) + ':00</option>';
echo '<option>' + sprintf ( "%02d", $h ) + ':30</option>';
}
If performance is what you're after, hardcoding the time strings is probably the best thing you can do.
Since they won't change, you don't really need them dynamic, would you?
foreach (range(0, 60*24, 30) as $minutes) {
echo '<option>' . date('H:i a', strtotime('00:00 +' . $minutes . ' minutes')) . '</option>';
}
Till day has 24h, you can use a good ol' for:
<select>
<?php for($i = 0; $i < 48; $i++): ?>
<option><?php echo $i%2 == 0 ? ($i / 2) . ':00' : intval($i / 2) .':30'; ?></option>
<?php endfor;?>
</select>
Related
I have a select drop down where I want to list times in the format of HH:MM with 1 minute intervals. so the list will start at 00:00 and finish at 23:59
I understand how to create a loop in a select drop down that will output 0-10
<select><?php for($i=0; $i<10; $i++){echo "<option>" . $i . "</option>";} ?>
</select>
and I understand how to output the time as HH:MM
<option><?php echo date('h:i', $supportrequest->startTime); ?</option>
But I can't work out how to do a combination of the two as I'm not sure what the parameters of the for loop should be
Using DatePeriod it'd be like that:
<?php
$begin = (new DateTime())->setTime(0,0,0); // create start point
$end = (new DateTime())->setTime(23,59,59); // create end point
$interval = new DateInterval('PT1M'); // set the interval to 1 minute
$daterange = new DatePeriod($begin, $interval ,$end); // create the DatePeriod
echo "<select>";
foreach($daterange as $date){ // loop through that period
echo "<option value='".$date->format("H:i") . "'>".$date->format("H:i")."</option>\n";
}
echo "</select>";
Using these classes makes it now easy to modify if you f.e. only want to have every 30 minutes, or need a different output format.
Do you really want to have a drop-down with one thousand, four hundred and forty option values (24 * 60 = 1440)? I think it would be better to have two <select> elements. You could style them to sit next to each other with a : in the middle if you wanted to keep the 'H:m' look.
<select id="hours">
<?php
for ($h = 0; $h < 24; $h++) printf("<option value=\"$h\"" . (!$h ? " selected" : "") . ">%02d</option>", $h);
?>
</select>
<select id="minutes">
<?php
for ($m = 0; $m < 60; $m++) printf("<option value=\"$m\"" . (!$m ? " selected" : "") . ">%02d</option>", $m);
?>
</select>
Converted Mukyuu's comment into an answer:
<select>
<?php
for($h=0; $h<24; $h++){
for($i=0; $i<60; $i++){
$time = date('h:i',strtotime($h.':'.$i));
echo "<option>".$time."</option>";
}
}
?>
</select>
I want to use the static keyword in my for loop.
Is it possible? if yes then how?
Here is my code:
$current_time = date('h:i A');
<select>
for($i = 0; $i <= 5; $i++) {
if ($i=0) {
echo "<option>" . date("h:i A", $current_time) . "</option>";
}else{
static $tNow = strtotime("+15 minutes",strtotime($current_time));
echo "<option>" . date("h:i A", $tNow) . "</option>";
}
}
<select>
When I'm using the static keyword, I'm getting a php error.
I want to display a <select> element with each option being 15 minutes steps, like 12:00, 12:15, 12:30.
I don't know why you want to use static keyword in your code, but as suggested you should read here: http://php.net/manual/en/language.oop5.static.php, how and when to use static keyword.
If your goal is to display dropdown and getting option value different 15mint time
like this 12:00, 12:15, 12:30..... this is my solution:
$current_time[0] = date('h:i A');
echo"<select>";
echo "<option>" . $current_time[0] . "</option>";
$step=0;
for($i = 1; $i <=5; $i++) {
$step=$step+15;
$current_time[$i] = strtotime("+".$step." minutes", strtotime($current_time[0]));
echo "<option>" . date("h:i A", $current_time[$i]) . "</option>";
}
echo"</select>";
var_dump($current_time);
Working code here: http://ideone.com/zM0eue
Explanation: I used an array to contain all strtotime output in for loop. Each time I used the same $current_time[0] to calculate new $current_time[$i] with a $step variable incremented by +15 each time.
I have code that generates a select dropdown with times from 8:00am-6:00pm. Upon selecting an option, and saving the form, the times are saved in the database properly. HOWEVER, I cannot retrieve the times automatically. This is my code so far :
<select value="<?php echo $starttime; ?>" name="data[InvoiceTime][<?php echo $key;?>][starttime]" id="starttime_<?php echo $key+1?>" class="form-control" autocomplete="off">
<?php
$time = mktime(0, 0, 0, 1, 1);
for ($i = 28800; $i < 42600; $i += 900) { // 1800 = half hour, 86400 = one day
printf('<option value="%1$sam">%1$sam</option>',
date('g:i', $time + $i), date('g:i a', $time + $i + 1800));
}
for ($i = 43200; $i < 65000; $i += 900) { // 12pm-6pm
printf('<option value="%1$spm">%1$spm</option>',
date('g:i', $time + $i), date('g:i a', $time + $i + 1800));
}
?>
</select>
Desired output : select "9:00am" and save form 9:00am is saved to db(which happens) reload form 9:00 appears preselected due to data saved in db
Actual output : select "9:00am" and save form 9:00am is saved to db reload form 8:00am appears(default value)
The following code works, but does not generate the option values :
<select value="<?php echo $starttime; ?>" name="data[InvoiceTime][<?php echo $key;?>][starttime]" id="starttime_<?php echo $key+1?>" class="form-control" autocomplete="off">
<option value="8:00am" <?= ($item['starttime']) == '8:00am' ? 'selected' : '' ?>>8:00am</option>
<option value="9:00am" <?= ($item['starttime']) == '9:00am' ? 'selected' : '' ?>>9:00am</option>
</select>
You just needed to move your check for the value into the existing code.
However, I have simplified your code quite a bit; I'm not sure what you were trying to accomplish with your printf() statements. gmdate() is happy to work with just seconds (you don't want to use date() as it can deliver unexpected results, based on your current time zone.) If an <option> element doesn't have a value attribute, the contents are used instead, so I've removed it. You should always always separate your PHP from your HTML (ideally more than I've done here, but this is a start.)
And in case you aren't familiar with them, here is some info on the ternary statement and heredoc blocks.
<?php
$start = 28800;
$stop = 65000;
$interval = 900;
$options = "";
for ($seconds = $start; $seconds <= $stop; $seconds += $interval) {
$time = gmdate("g:ia", $seconds);
$selected = ($item["starttime"] === $time) ? " selected" : "";
$options .= sprintf("<option %s>%s</option>", $selected, $time);
}
echo <<< HTML
<select value="$starttime" name="data[InvoiceTime][$key][starttime]" id="starttime_$key" class="form-control" autocomplete="off">
$options
</select>
HTML;
I have two selects, one that is a time from and the other is a time to. These values are split by fifteen minute integrals. I take those values and convert them using PHP's strtotime. I would like to compare these values to make sure that the time to is not lower than the time from or that the time from is not higher than the time to using jQuery's validate.
Thanks.
Here is my PHP:
$start = strtotime('12:00 AM');
$end = strtotime('12:00 PM');
echo '<select name="TimeFrom" id="TimeFrom" class="timeSelectFrom">';
for ($i = $start; $i <= $end; $i += 900)
{
echo '<option>' . date('g:i A', $i) . '</option>';
}
echo '<option>Closed</option>';
$start = strtotime('12:00 AM');
$end = strtotime('12:00 PM');
echo '<select name="TimeTo" id="TimeTo" class="timeSelectTo">';
for ($i = $start; $i <= $end; $i += 900)
{
echo '<option>' . date('g:i A', $i) . '</option>';
}
echo '<option>Closed</option>';
echo '</select>';
You could easily translate the values from the form elements into a basic time-stamp using a simplistic formula like (seconds+60*minutes) and then compare them as required.
I have really been trying hard to achieve the following:
<option value="2011-11"><? echo $month[11]?> '11</option>
<option value="2011-12"><? echo $month[12]?> '11</option>
<option value="2012-1"><? echo $month[1]?> '12</option>
<option value="2012-2"><? echo $month[2]?> '12</option>
Where the following are dynamic according to today's date:
<option value="YYYY-m"><? echo $month[12]?> 'yy</option>
and repeating the process for the following 12 months ahead of today.
I made some progress with my rudimentary knowledge of PHP, I have admitted I need help from those more knowledgeable than myself!
It does look simple and I often look at stackoverflow to solve things though on this occasion I have spent far too much time and made too little progress.
Can anyone give me a hand to this seemingly simple challenge!?
Thank you!!!
Commenting for anybody who finds question.
The accepted answer is wrong. See here:
http://www.php.net/manual/en/function.strtotime.php#107331
use this instead:
<?php
for($i=0;$i<=12;$i++)
{
$time = strtotime("first day of +". $i." months");
print '<option value="'. date("Y-m", $time) .'">'.date("Y-m",$time).'</option>';
}
?>
The fix being "first day of"
This snippet is proof of concept achieving exactly what you want.
for($i=0;$i<=12;$i++)
{
$time = strtotime("today + ". $i." months");
print '<option value="'. date("Y-m", $time) .'">'.date("Y-m",$time).'</option>';
}
If you want to print the name of the month in your specific language, you should take a look at setlocale and just use the F parameter in date
Here you go:
$time = time();
foreach (range(0, 12) as $i)
{
echo '<option value="' . date('Y-m', $time) . '">' . $month[date('m', $time)] . date('y', $time) . '</option>' . "\n";
if ($i >= 1)
{
$time = strtotime('next month', $time);
}
}