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>
Related
Here's my code that display date with for loop.
Topic: Im creating a script to generate payment due(from to start).
$y = 1;
$period = 3;
$start = date('m/15/Y');
echo "<table>";
echo '<thead><th>From</th>';
echo '<th>To</th></thead>';
for ($y; $y <= $period; $y++) {
$month_mid = date("m/15/Y", strtotime($start));
$month_last = date("m/t/Y", strtotime($start));
echo '<td>'.$month_mid = date("m/t/Y", strtotime($start)).'</td>';
echo '<td>'.$month_last = date("m/15/Y", strtotime($start)).'</td></tr>';
$start = date("m/d/Y",strtotime($start." +1month"));
}
echo '</table>';
output I get:
09/15/2017 09/30/2017
10/15/2017 10/31/2017
11/15/2017 11/31/2017
I want to appear like this:
09/15/2017 09/30/2017
09/30/2017 10/15/2017
10/15/2017 10/31/2017
Im new in date php hope you can help me with this thanks.
Here you go:
$y = 1;
$period = 5;
$start = date('m/15/Y');
echo "<table>";
echo '<thead><th>From</th>';
echo '<th>To</th></thead>';
for ($y; $y <= $period; $y++) {
$month_mid = date("m/15/Y", strtotime($start));
$month_last = date("m/t/Y", strtotime($start));
echo '<tr><td>'.$month_mid = date("m/t/Y", strtotime($start)).'</td>';
echo '<td>'.$month_last = date("m/15/Y", strtotime($start)).'</td></tr>';
$start = date("m/d/Y",strtotime($start." +1month"));
}
echo "</table>";
You missed the opening and clousure of table and <tr>.
You don't want to be skipping ahead by a month in your loop. Well, not the way you are doing it here.
You should use DatePeriod::getEndDate and DatePeriod::getStartDate, along with DateTime::add to skip by semi-monthly amounts. The idea is that you only get to the next month by letting the datetime API add 15 days to a given start date, and use that to figure out the mid-month and end-month dates.
Keep everything in these date objects until you need to format and print them, and save the second one in the pair as input for the next round of the loop at the end where you just have to calculate the new second value.
I feel like you could write a function that gets the "next" date from any other first or last date of the month to simplify the loop.
(Since this is semi-monthly and not bi-weekly, you can actually just walk the months in your period, hard-coding the 15th for one value and using your end-of-month function for the second. It depends on your requirements and how complicated the API gets.)
Or
For each month in your period, calculated the mid-month (i.e., exactly 15 days from the start of the month) and end-month dates. Save them in a Collection of couplets of some sort.
Write a display routine that takes this Collection and outputs the dates, but saves the previous formatted string made from the second item in the couplet as the first item to be printed (after the first line.)
I actually prefer this one because it separates the presentation from the data abstraction, allowing you freedom to display and format the date how you see fit.
But, at the end of the day (pun not intended, but what a great pun), stop using date strings as input to figure out other dates when you have access to normalized epoch representations. This will only lead to madness.
$y = 1;
$period = 3;
$start = date('m/d/Y');
$end = date('m/t/Y');
echo "<table>";
echo '<thead><th>From</th>';
echo '<th>To</th></thead>';
for ($y; $y <= $period; $y++) {
echo '<tr><td>'.$start.'</td>';
echo '<td>'.$end.'</td></tr>';
$getLast = date('d',strtotime($end));
if($getLast >= 28) {
$start = date("m/t/Y", strtotime($start));
$end = date("m/d/Y", strtotime("+15 day", strtotime($end)));
}else {
$start = date("m/d/Y", strtotime("+15 day", strtotime($start)));
$end = date("m/t/Y",strtotime($end));
}
}
echo "</table>";
Result:
From To
09/15/2017 09/30/2017
09/30/2017 10/15/2017
10/15/2017 10/31/2017
RESOLVED in answer below
Creating a scheduling tool for my users and I am having an issue trying to increment my variable obtained from a dropdown selection:
$date_select = $_POST['date_select'];
I'm sure it's a textbook fix, but to put it simply...I need to increment $date_select by +1 week for 52 weeks.
I have a dropdown menu starting with the current date, and looping to the end of 365 days, incrementing by 1. No problem here.
<select name="date_select" form="create_schedule">
<?php
for($i = 0; $i <= 365; $i++){
$d=strtotime($i . " Day");
$day = date("n-j-y l", $d) . "<br>";
echo "<option>" . $day . "</option>";
}
?>
</select>
This selection is represented by:
$date_select = $_POST['date_select'];
Next to that, before submitting, users can select a radio button - either M, T, W, Th, F, Sat, or Sun to indicate if they would like to apply their request to that selected day, for every week, for the rest of the year. (Which is what I'm trying to do...which is: increment $date_select by "+1 Week" until the for loop is finished)
This selection is represented by:
$repeat = $_POST['repeat'];
This is the closest I've gotten...the code below increments for every "Monday" like I want for example...if $repeat == 'M', but the numerical dates are wrong...
if(isset($_POST['repeat'])){
for($i = 0; $i <= 52; $i++){
$date = strtotime($i . " week", strtotime($date_select));
echo date("n-j-y l", $date) . "<br/>";
}
For example: if the date selected is 7-4-16 Monday, the output is this:
11-26-07 Monday
12-3-07 Monday
12-10-07 Monday
12-17-07 Monday
12-24-07 Monday
12-31-07 Monday
1-7-08 Monday
And so forth...
Thank you in advance.
RESOLVED The issue was in the date format..."m-d-Y" is not equivalent to "m/d/Y" when incrementing days weeks or months in regards to how it is output. Somewhere along the lines, "American" date format values and "European" date format values were getting mixed up. I changed the date format within both of the for-loops and got it working.
"Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible."
http://www.w3schools.com/php/func_date_strtotime.asp
Here is the working solution in case anyone is trying to do something similar
<select name="date_select" form="create_schedule">
<?php
for($i = 0; $i <= 365; $i++){
$d=strtotime($i . " Day");
$day = date("m/d/Y l", $d) . "<br>";
echo "<option>" . $day . "</option>";
}
?>
</select>
if(isset($_POST['repeat'])){
$repeat = $_POST['repeat'];
echo "<br/>";
for($i = 0; $i <= 13; $i++){
$d=strtotime($i . " week", strtotime($date_select));
echo date("m/d/Y l", $d) . "<br/>";
}
}
So on PHP you have to classes that can be really helpfull doing that
\DateTime and \DateInterval
So to do what you want I would recommend
$firstDate = \DateTime::createFromFormat('Y-m-d', $date_select));
$baseDate = clone $firstDate;
$intervalToAdd = new \DateInterval('P1w')
if(isset($_POST['repeat'])){
for($i = 0; $i <= 52; $i++){
$date [$i] = $baseDate->add($intervalToAdd);
echo '<option>'.$date[$i]->format('Y-m-d').'</option>';
}
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.
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>
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.