Use static keyword in a for loop? - php

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.

Related

For loop time update(H:i) with 10 minutes in php

I am trying to update time in for loop in php and i am getting unexpected results.
I have tried using while loop still same results. Dont know where its going wrong.
$time = strtotime(date('H:i'));
$trounds= 15;
for($j = 1; $j<= $trounds ; $j++){
echo $time += date("H:i:s", strtotime("+10 minutes", $time));
echo date('H:i:s',$time);
echo '</br>';
}
Suppose if the time is 12:00 i need output like
12:10
12:20
12:30
and so on.
I think you don't need date() and strtotime() twice , Try like below
$time = strtotime(date('4:i'));
$trounds= 15;
for($j = 1; $j<= $trounds ; $j++){
echo $time += 10*60;
echo ' : '.date('H:i:s',$time);
echo "\n";
}
Live Example
Output :
1563017220 : 04:27:00
1563017820 : 04:37:00
1563018420 : 04:47:00
1563019020 : 04:57:00
1563019620 : 05:07:00
1563020220 : 05:17:00
1563020820 : 05:27:00
1563021420 : 05:37:00
1563022020 : 05:47:00
1563022620 : 05:57:00
1563023220 : 06:07:00
1563023820 : 06:17:00
1563024420 : 06:27:00
1563025020 : 06:37:00
1563025620 : 06:47:00
<?php
$date = new DateTime();
echo "Current Date Time: "; print_r($date); echo "</br>";
$trounds= 15;
for($j = 0; $j< $trounds ; $j++){
$date->modify("+10 minutes");
print_r($date->format('H:i')); echo '</br>';
}
exit;
Use PHP DateTime Class For Better Usage and Flexibility. You can read and learn more about from PHP official docs : PHP DateTime Class. You can make use of various other methods that are provided by the DateTime Class.
You are adding the incorrect values in your $time after the just after the for loop which is making it wrong
$time = strtotime(date('H:i'));
$trounds= 15;
for($j = 1; $j<= $trounds ; $j++){
echo $time = strtotime("+10 minutes", $time); //echo $time += date("H:i:s", strtotime("+10 minutes", $time));
echo date('H:i:s',$time);
echo '</br>';
}
You're adding the string format ("H:i:s") value into the float value.. Check the above code you just need to assign the strtotime value and it will do rest.

How to loop through time in select box PHP

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>

How can I echo out the selected date

does anyone know how I would echo out the selected date as text with the date month and year separated outside of the form? I tried echoing out $date $month and $year outside of the form however this doesn't give me the correct date thankyou for the help
<?
$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28',
'16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
'16-11-14','16-11-28','16-12-14','16-12-28');
$currentdate = date('y-m-d');
echo $currentdate;
?>
<form>
<select style="width:200px;">
<?php
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
$selected = "selected";
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
echo 'the current billing period is';
}
?>
</select>
</form>
Inside of your loop add a $selected_int variable like so:
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
$selected = "selected";
$selected_int = $i;
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
echo 'the current billing period is';
}
Then, you can reference it like:
echo date('Y-m-d', strtotime($date[$selected_int]));
Addition
I know you've already accepted the answer, but I also wanted to make a suggestion now that I see what you are using the $date for. Since you know the start date, and it is in 14-day periods, it would be easy to write that as part of the loop.
$start_date = date('Y-m-d', strtotime(date('Y').'-01-01'); //First day of the year, for the sake of argument.
$interval = 14;
for ($i = 0; date('Y') == date('Y', strtotime($start_date.' +'.($i * $interval).' days')); $i++) {//While this year is equal to the start date's year with the added interval [If I knew what your logic here was I could offer a better suggestion]
if ($currentdate >= date("Y-m-d", strtotime($start_date.' +'.($i * $interval).' days')) && (date('Y') < date("Y", strtotime($start_date.' +'.(($i + 1) * $interval).' days')) || $currentdate < date("m/d/Y", strtotime($start_date.' +'.(($i + 1) * $interval).' days')))) {
$selected = "selected";
$selected_int = $i;
} else {
$selected = "";
}
echo "<option $selected>" . date("m/d/Y", strtotime($start_date.' +'.($i * $interval).' days')) . "</option>";
}
Basically, this takes the start date, shows it as the first date option, then adds 14 days to it with each pass through. Your if/else statement should still be the same. It checks to see if you are on the last interval of the year, or if the current date is less than the next interval, and also that the current date is greater than the current interval.
After your loop, you can get the date by:
echo date("m/d/Y", strtotime($start_date.' +'.($selected_int * $interval).' days'));
I know it seems like a lot, but it would save you from having to make a date array to begin with.
Use strtotime instead list.
....
// list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
....
EDIT: Additional information - your code requires a lot modification and likely some structure changes but assuming this is for testing a method and "how to do" instead a final product.
You need to submit the selected date, catch it in the script and use the selected date to do what you need - i.e. retrieve data from database - and this should give you some idea.
<?php
// You need to create these dates by using another method. You cannot hard code these. You can create it with date functions easily.
$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28','16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28','16-11-14','16-11-28','16-12-14','16-12-28');
// Checking if we have a posted form, with the button name user clicked
if (isset($_POST["btnSubmit"])) {
// This is your selected day - use it where you need:
$selectedDate = $_POST["selectedDate"];
// This is where your model start singing and gets necessary info for this date - just printing here as sample
print $selectedDate;
// I need dropDownDate to compare in the SELECT to preselect the appropriate date
$dropDownDate = strtotime($selectedDate);
} else {
// First time visit, preselect the nearest date by using current date
$dropDownDate = time();
}
?>
<form method="post">
<select name="selectedDate" style="width:200px;">
<?php
foreach ($date as $i => $d) {
if ($dropDownDate >= strtotime($d) &&
(!isset($date[$i+1]) || ($dropDownDate < strtotime($date[$i+1])))
) {
$selected = 'selected="selected"';
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
}
?>
</select>
<input type="submit" name="btnSubmit" value="Submit">
</form>
Note that I added a "submit" type input (to submit the form) and changed form method to "post", finally named SELECT as "selectedDate". I also changed your date comparison code line in the loop.
Hope this helps.

Performance tune PHP Time Query

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>

Compare two time values using jQuery's validate

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.

Categories