How to make dynamic time in select box with 5 minutes interval? - php

I need dynamic time select box with 5 minutes interval in php. Anybody help me?
<select name="time" id="time">
<option value="5:00 AM">5:00 AM</option>
<option value="5:05 AM">5:05 AM</option>
<option value="5:10 AM">5:10 AM</option>
.
.
.
.
<option value="23:55 AM">23:55 AM</option>
</select>

With DateTime:
$dateTime = new DateTime('5:00 AM');
for($i=0; $i <= 515; $i++) {
echo $dateTime->format('H:i A'), "\n";
// add 5 minutes
$dateTime->modify('+5 minutes');
}
You can use modify to add how many minutes you want.

Related

how to disable an option if it's already selected (PHP - MYSQL)

i am working on a dental booking website. it's a school project and i am almost done. but i have 1 major problem and would really appreciate your help.. i have this booking page wherein the client can set date, time and service. on my time, i have set a dropdown for available hours. i would want those hours to be available per day. i was wondering if it is possible to disable an option if it is already booked for that day. i only have these..
i have a table called appointments. columns are ID, userID, reservationDateTime, schedDate, schedTime, service..
<?php
$time = array('8:00 AM', '9:00 AM', '10:00 AM', '11:00 AM','1:00 PM','2:00 PM','3:00 PM','4:00 PM',);
?>
<p>
<label>Time</label><br>
<select name="appTime">
<option>Select Time</option>
<?php
foreach($time as $value){
echo "<option>$value</option>";}
?>
</select>
</p>
<?php
unset($time)
?>
var $selects = $('select');
$selects.on('change', function() {
var $select = $(this),
$options =
$selects.not($select).find('option'),selectedText =
$select.children('option:selected').text();
var $optionsToDisable = $options.filter(function() {
return $(this).text() == selectedText;
});
$optionsToDisable.prop('disabled', true);
});
//to apply initial selection
$selects.eq(0).trigger('change');
echo "<option disabled>$value</option>";
You can use disabled attribute. A disabled option is unusable and un-clickable. This disable all the options (you will like to add some condition...)
<?php
foreach($time as $value){
echo "<option disabled >$value</option>";
}
?>
You could do this:
echo "<option disabled>$value</option>";
<select>
<option value="volvo" disabled>8:00 AM</option>
<option value="saab">9:00 AM</option>
<option value="vw">10:00 AM</option>
<option value="audi">11:00 AM</option>
</select>
If this times are fixed time that you have to show then directly show in option
<p>
<label>Time</label><br>
<select name="appTime">
<option>Select Time</option>
<?php
foreach($time as $time){
?>
<option value="1" <?php if (in_array(1, $time)) {echo "disabled=\"disabled\"";}?>>8:00 AM</option>
<option value="2" <?php if (in_array(2, $time)) {echo "disabled=\"disabled\"";}?>>9:00 AM</option>
<option value="3" <?php if (in_array(3, $time)) {echo "disabled=\"disabled\"";}?>>10:00 AM</option>
<?php }?>
</select>
</p>
Maintain table to save appointment fetch this from table and check is it available if yes the show else disable it.

Create dynamic <select> with PHP

I have a $_SESSION variable $_SESSION['invoiceDate'] that is populated with a value representing YYYYMM.
I need to dynamically build a select element so that I have 6 options going back from the 'invoiceDate' e.g. if $_SESSION['invoiceDate'] = 201602 the output should be...
<select id="invoiceDate" name="invoiceDate">
<option value="201602">Feb 2016</option>
<option value="201601">Jan 2016</option>
<option value="201512">Dec 2015</option>
<option value="201511">Nov 2015</option>
<option value="201510">Oct 2015</option>
<option value="201509">Sep 2015</option>
</select>
My PHP is very basic... I can split the session value and assign the first 4 digits to $year and the other 2 to $month. Then loop 6 times... Each time I echo to output the <option> line concatenating the year and month to make the value and then subtracting 1 from the month and if month=0 then take 1 from year and make month 12. This gives me....
<option value="201602"></option>
<option value="201601"></option>
<option value="201512"></option>
<option value="201511"></option>
<option value="201510"></option>
<option value="201509"></option>
Now I can join the year and month with a hyphen to give
<option value="201602">2016-02</option>
<option value="201601">2016-01</option>
<option value="201512">2015-12</option>
<option value="201511">2015-11</option>
<option value="201510">2015-10</option>
<option value="201509">2015-09</option>
But how do I get a Month Year instead... e.g. Feb 2016 instead of 2016-02?
Why aren't you using DateTime?
$e = '201602';
$date = new DateTime($e);
echo $date->format('M');
outputs Feb
In your case it could be
$e = '201602';
$date = new DateTime($e);
$month = $date->format('n');
for($i=0;$i<6;$i++)
{
echo '<option value="'.$date->format('Ym').'">'.$date->format('M Y').'</option>';
$date = $date->sub(new DateInterval('P1M'));
}
This outputs
<option value="201602">Feb 2016</option>
<option value="201601">Jan 2016</option>
<option value="201512">Dec 2015</option>
<option value="201511">Nov 2015</option>
<option value="201510">Oct 2015</option>
<option value="201509">Sep 2015</option>
Just edit this as you need and don't use that infernal version of date with strtotime...
$date = "201602";
for($i=1;$i<=6;$i++){
echo date('M, Y',strtotime(date('Y-m-d',strtotime($date))." -$i month"));
}
In this loop we are deleting 1 month from your date.
Date Parameters
This is it:
<?php $session_val = "201602";?>
<select id="invoiceDate" name="invoiceDate">
<?php
for($i = 0; $i < 6; $i++){
//$text = date('Y-m', strtotime(date('Y-m-d', strtotime($session_val))." -$i month")); // for 2016-02
$text = date('M Y', strtotime(date('Y-m-d', strtotime($session_val))." -$i month")); //for Feb 2016
$val = date('Ym', strtotime(date('Y-m-d', strtotime($session_val))." -$i month"));
echo '<option value="'.$val.'">'.$text.'</option>';
}?>
</select>
I did this in my local, but when i am going to post i see some one
post similar thing. But I did so i post.

Calculate total earnings while looping between two points of time every 15 minutes

I am really stuck on the code, and I dont know how to solve it. I am trying to calculate how much I earn a day. The problem is that I earn different amount depending on which hour on a day it is. Fx:
08.00 - 06.00 (Morning) -> 150
06.00 - 10.00 (Evening) -> 200
10.00 - 11.00 (Night) -> 250
So I have calculated how much hours I work a day with this code:
<p>Choose Your Work TIme</p>
<form method='post'>
<select name="s1" required>
<option>Choose Start Time</option>
<option value="8.45">8.45</option>
<option value="9.00">9.00</option>
<option value="9.15">9.15</option>
<option value="9.30">9.30</option>
<option value="9.45">9.45</option>
<option value="10.00">10.00</option>
<option value="10.15">10.15</option>
<option value="10.30">10.30</option>
<option value="10.45">10.45</option>
<option value="11.00">11.00</option>
<option value="11.15">11.15</option>
<option value="11.30">11.30</option>
<option value="11.45">11.45</option>
<option value="12.00">12.00</option>
<option value="12.15">12.15</option>
<option value="12.30">12.30</option>
<option value="12.45">12.45</option>
<option value="13.00">13.00</option>
<option value="13.15">13.15</option>
<option value="13.30">13.30</option>
<option value="13.45">13.45</option>
<option value="14.00">14.00</option>
<option value="14.15">14.15</option>
<option value="14.30">14.30</option>
<option value="14.45">14.45</option>
<option value="15.00">15.00</option>
<option value="15.15">15.15</option>
<option value="15.30">15.30</option>
<option value="15.45">15.45</option>
<option value="16.00">16.00</option>
<option value="16.15">16.15</option>
<option value="16.30">16.30</option>
<option value="16.45">16.45</option>
<option value="17.00">17.00</option>
<option value="17.15">17.15</option>
<option value="17.30">17.30</option>
<option value="17.45">17.45</option>
<option value="18.00">18.00</option>
<option value="18.15">18.15</option>
<option value="18.30">18.30</option>
<option value="18.45">18.45</option>
<option value="19.00">19.00</option>
<option value="19.15">19.15</option>
<option value="19.30">19.30</option>
<option value="19.45">19.45</option>
<option value="20.00">20.00</option>
<option value="20.15">20.15</option>
<option value="20.30">20.30</option>
<option value="20.45">20.45</option>
<option value="21.00">21.00</option>
<option value="21.15">21.15</option>
<option value="21.30">21.30</option>
<option value="21.45">21.45</option>
<option value="22.00">22.00</option>
<option value="22.15">22.15</option>
<option value="22.30">22.30</option>
<option value="22.45">22.45</option>
<option value="23.00">23.00</option>
<option value="23.15">23.15</option>
<option value="23.30">23.30</option>
</select>
<select name="s2" required>
<option>Choose End Time</option>
<option value="8.45">8.45</option>
<option value="9.00">9.00</option>
<option value="9.15">9.15</option>
<option value="9.30">9.30</option>
<option value="9.45">9.45</option>
<option value="10.00">10.00</option>
<option value="10.15">10.15</option>
<option value="10.30">10.30</option>
<option value="10.45">10.45</option>
<option value="11.00">11.00</option>
<option value="11.15">11.15</option>
<option value="11.30">11.30</option>
<option value="11.45">11.45</option>
<option value="12.00">12.00</option>
<option value="12.15">12.15</option>
<option value="12.30">12.30</option>
<option value="12.45">12.45</option>
<option value="13.00">13.00</option>
<option value="13.15">13.15</option>
<option value="13.30">13.30</option>
<option value="13.45">13.45</option>
<option value="14.00">14.00</option>
<option value="14.15">14.15</option>
<option value="14.30">14.30</option>
<option value="14.45">14.45</option>
<option value="15.00">15.00</option>
<option value="15.15">15.15</option>
<option value="15.30">15.30</option>
<option value="15.45">15.45</option>
<option value="16.00">16.00</option>
<option value="16.15">16.15</option>
<option value="16.30">16.30</option>
<option value="16.45">16.45</option>
<option value="17.00">17.00</option>
<option value="17.15">17.15</option>
<option value="17.30">17.30</option>
<option value="17.45">17.45</option>
<option value="18.00">18.00</option>
<option value="18.15">18.15</option>
<option value="18.30">18.30</option>
<option value="18.45">18.45</option>
<option value="19.00">19.00</option>
<option value="19.15">19.15</option>
<option value="19.30">19.30</option>
<option value="19.45">19.45</option>
<option value="20.00">20.00</option>
<option value="20.15">20.15</option>
<option value="20.30">20.30</option>
<option value="20.45">20.45</option>
<option value="21.00">21.00</option>
<option value="21.15">21.15</option>
<option value="21.30">21.30</option>
<option value="21.45">21.45</option>
<option value="22.00">22.00</option>
<option value="22.15">22.15</option>
<option value="22.30">22.30</option>
<option value="22.45">22.45</option>
<option value="23.00">23.00</option>
<option value="23.15">23.15</option>
<option value="23.30">23.30</option>
</select>
<input type="submit" name="submit" Value="Submit">
</form>
Lets say I work from 10.00 in the morning to 11.00 in the night. That would mean I work 13 hours. But how can i calculate how much money I earn in the above time period? I have tried the following but nothing is working:
<?php
// create timeblocks:
// 8 = from 0.00 untill 8.00
// 18 = from 8.00 untill 18.00
// 24 = from 18.00 untill 0.00
$timeblocks=array(
8*60,
18*60,
24*60
);
//earnings PER 15 MINUTES in the above timeblocks
$money=array(
146/4,
204/4,
244/4
);
$earned=0;
//create a start and end time object
$start=date_create_from_format('Y-m-d H:i:s', $_POST['s1']);
$end=date_create_from_format('Y-m-d H:i:s', $_POST['s2']);
//calculate how many blocks of 15 minutes
$diff=$start->diff($end);
$steps=(($diff->d)*3600+($diff->h)*60+($diff->i))/15;
//loop every 15 minutes
for($i=0;$i<=$steps;$i++){
//start with the start date, and add 15 minutes every time
if($i!==0)$start->add(new DateInterval('PT15M'));
//calculate the time
$time=((int)($start->format('H')*60)+(int)($start->format('i')));
//look in timeblocks what block we are in and get the earnings from that block
for($block=0;$block<3;$block++){
if($time<=$timeblocks[$block]){
$earned+=$money[$block];
echo '$'.$money[$block].' earned at '.$start->format('d-m-Y H:i:s').'<br>';
break;
}
}
}
echo 'TOTAL earned: $'.$earned;
?>
I hope somebody can help me.
Best Regards
Mads
I'm sure there are better solutions, but this works :-)
[edit] to meet OP's new requirements
//If you haven't done allready SET your timezone!!
date_default_timezone_set('Europe/Amsterdam');
// create timeblocks:
// 8 = from 0.00 untill 8.00
// 18 = from 8.00 untill 18.00
// 24 = from 18.00 untill 0.00
$timeblocks=array(
8*60,
18*60,
24*60
);
//earnings PER 15 MINUTES in the above timeblocks
$money=array(
100/4,
10/4,
50/4
);
$earned=0;
$_POST['s1']='18.30';
$_POST['s2']='8.30';
//sanitize your post: only numbers and a dot allowed
$s1=preg_replace("/[^0-9|.]/","",$_POST['s1']);
$s2=preg_replace("/[^0-9|.]/","",$_POST['s2']);
if($s1==$s2){
//no working time: do something
//NOTE: this assumes nobody working for 24 hours, because if you start at
//6pm day 1 and end 6pm on the next day (==24 hours) the times are the same
//as well.
}
//if second time smaller then first time, we are in the next day and add 24 hours
$add=0;
if($s1>$s2)$add=24;
//make hours and minutes
$s1=explode('.',substr('0'.$s1,-5));
$s2=explode('.',substr('0'.$s2,-5));
//create a start and end time object
$start=new Datetime('now');
$start->setTime($s1[0],$s1[1]);
$end=new Datetime('now');
$end->setTime($s2[0]+$add,$s2[1]);
//calculate how many blocks of 15 minutes
$diff=$start->diff($end);
$steps=(($diff->d)*3600+($diff->h)*60+($diff->i))/15;
//loop every 15 minutes
for($i=1;$i<=$steps;$i++){
//start with the start date, and add 15 minutes every time
$start->add(new DateInterval('PT15M'));
//calculate the time
$time=((int)($start->format('H')*60)+(int)($start->format('i')));
//look in timeblocks what block we are in and get the earnings from that block
for($block=0;$block<3;$block++){
if($time<=$timeblocks[$block]){
$earned+=$money[$block];
echo '$'.$money[$block].' earned at '.$start->format('d-m-Y H:i:s').'<br>';
break;
}
}
}
echo 'TOTAL earned: $'.$earned;
$begin = new DateTime( 'YYYY-MM-DD h:I' );
$end = new DateTime( 'YYYY-MM-DD h:I' );
$interval = new DateInterval('P1I');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
//each of the hours in between.
//do the checks on the hour here $date will be the variable your interested in
}
I would suggest using the date time, Hours get more complex when DST etc come in to play also when crossing midnight
It would be easier to change your HTML code replacing your "dot-minutes" values with .25, .5 and .75 in the value attribute of each option tag. For instance :
<option value="13.75">13:45</option>
The following PHP code will then do what you are expecting
Your original code:
<?php
if (isset($_POST['submit'])) {
echo "Started time : " . $_POST['s1'] . "<br/>";
echo "End time : " . $_POST['s2'] . "<br/>" ;
$start_time = $_POST['s1'];
$end_time = $_POST['s2'];
$early_time_salary = 150;
$middle_time_salary = 200;
$late_time_salary = 250;
Code to add
$hour0=$end_time-$start_time;
if ($hour0>0) {
$hour1=($end_time>18) ? $end_time-18 : 0;
$hour2=($end_time>22) ? $end_time-22 : 0;
$tot=($hour0*150) + $hour1*50 + $hour2*50;
echo "Total earned: $tot";
} else {
echo "Error : work finished before it starts";
}
}
I would approach it this way:
Create a lookup array with elements for each time-based pay change.
Create a DateTime Period to iterate every 15 minutes between the two passed in times.
As you iterate each 15-minute interval, calculate the correct pay and add that amount to your total.
Code: (Demo)
$_POST['s1'] = '16.15';
$_POST['s2'] = '23.30';
$wagesLookup = [
'18:00' => 146 / 4, // morning
'22:00' => 204 / 4, // evening
'24:00' => 244 / 4 // night
];
$period = new DatePeriod(
DateTime::createFromFormat('H.i', $_POST['s1']),
new DateInterval('PT15M'),
DateTime::createFromFormat('H.i:01', "{$_POST['s2']}:01") // bump ahead to make "inclusive"
);
$total = 0;
foreach ($period as $dt) {
$time = $dt->format('H:i');
foreach ($wagesLookup as $mark => $pay) {
if ($time <= $mark) {
break;
}
}
printf("%s earned %.02f\n", $dt->format('H:i'), $pay);
$total += $pay;
}
echo "Total: $total";
Output:
16:15 earned 36.50
16:30 earned 36.50
16:45 earned 36.50
17:00 earned 36.50
17:15 earned 36.50
17:30 earned 36.50
17:45 earned 36.50
18:00 earned 36.50
18:15 earned 51.00
18:30 earned 51.00
18:45 earned 51.00
19:00 earned 51.00
19:15 earned 51.00
19:30 earned 51.00
19:45 earned 51.00
20:00 earned 51.00
20:15 earned 51.00
20:30 earned 51.00
20:45 earned 51.00
21:00 earned 51.00
21:15 earned 51.00
21:30 earned 51.00
21:45 earned 51.00
22:00 earned 51.00
22:15 earned 61.00
22:30 earned 61.00
22:45 earned 61.00
23:00 earned 61.00
23:15 earned 61.00
Total: 1413

PHP: Convert back to time() format from form select

Not sure if this has already been answered or not, but I did do a google search and couldn't really find anything useful.
I have the following html code (as an example) which has a form select field for month, day, year, hour, and minute (second is always 00) and I need to convert that back to time() format on form submit. How would I do this with php?
<select name="month">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
...
</select>
<select name="day">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
...
</select>
<select name="year">
<option value="2014">2014</option>
<option value="2015">2015</option>
...
</select>
<select name="hour">
<option value="00">12am</option>
<option value="01">1am</option>
<option value="02">2am</option>
<option value="03">3am</option>
<option value="12">12pm</option>
<option value="13">1pm</option>
...
</select>
<select name="minute">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
...
</select>
<!-- second is always 00 -->
Note: The values of each select can change if they need to. Not sure what format they need to be in to make this work efficiently.
$time = DateTime::createFromFormat(
'Y-m-d H:i:s',
sprintf("%d-%d-%d %d:%d:%d",
intval($_POST['year']),
intval($_POST['month']),
intval($_POST['day']),
intval($_POST['hour']),
intval($_POST['minute']),
0
)
);
You now have a DateTime object, to get a unix timestamp do:
$timestamp = $time->getTimestamp();
Check out strtotime() and the DateTime class.
<?php
$date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'];
$time = $_POST['hour'] . ':' . $_POST['minute'];
// Simple way to get a Unix timestamp from a date string
$timestamp = strtotime($date . ' ' . $time);
// To do more modifications to the date, you should use the DateTime class
$dateObj = new DateTime($date . ' ' . $time);
$dateObj->modify('+1 year'); // Adds a year to the date
$timestamp = $dateObj->format('U'); // Returns the date as a Unix timestamp
Use strtotime function.
echo strtotime("10 September 2000"); // output 968569200 of unix timestamp

Get date, explode, use data to select options in <select>

I have three variables set, $date_legible (in the format of F jS, Y, i.e. January 1st, 2011), $date_timestamp (self-explanatory), and $date_normal (in the format of YYYY-MM-DD)
I have three select fields:
<select name="date_mo">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="date_dy">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="date_yr">
<?php for($year=$curryear;$year<$curryear+51;$year++){ ?>
<option value="<?php echo $year; ?>"><?php echo $year; ?></option>
<?php } ?>
</select>
I know I want to convert the timestamp, but I will ultimately need to explode the output for the $month, $day, and $year. The problem is I don't know how to check the selects to the values and automatically select the correct option.
Can anyone help? Thanks!
UPDATE
I now have one more variable, because I'm having users submit from the front end, and Gravity Forms submits as YYYY-MM-DD.
Prior to diving into the solutions already posted (thank you guys [and gals] by the way), here's a little more information:
I'm running WordPress and have three custom fields as described above. When a user submits a post from the front end, the only custom field that's filled out is $date_normal. This is okay, because a post has to be moderated (i.e. published) from the backend. What I NOW need is on load to set the YYYY-MM-DD to set the options on the select. I'm not asking you guys to alter your answers, I'm sure I can figure it out (though if you can help with this new problem, it would be greatly appreciated).
If I understand your question correctly, you have a timestamp and you need to use the timestamp to select the appropriate select options when you render the page.
As a forewarning - this will make it possible to select invalid dates, for example February 31st (does not exist). You may want to consider re-drawing the year drop-down when the month is changed using javascript. Since you didn't specify any javascript library (and it is not clear how important the possibility of an invalid date is), I will not venture to show code for that, but it would not be terribly difficult.
// using the current time for the sake of the example, your timestamp would take the place of this
$timestamp = time();
// determine the selected month, day, and year
$selected_month = date('n', $timestamp);
$selected_day = date('j', $timestamp);
$selected_year = date('Y', $timestamp);
// now, create the drop-down for months
$month_html = '<select name="date_mo">';
for ($x = 1; $x < 13; $x++) {
$month_html .= '<option value='.$x.($selected_month == $x ? ' selected=true' : '' ).'>'.date("F", mktime(0, 0, 0, $x, 1, $selected_year)).'</option>';
}
$month_html .= '</select>';
// output
print $month_html;
// create the day drop-down
$day_html = '<select name="date_day">';
for ($x = 1; $x < 32; $x++) {
$day_html .= '<option value='.$x.($selected_day == $x ? ' selected=true' : '' ).'>'.$x.'</option>';
}
$day_html .= '</select>';
// output
print $day_html;
// create the year drop-down
$year_html = '<select name="date_year">';
$start_year = date('Y', time());
$max_year = $start_year + 51;
for ($x = $start_year; $x < $max_year; $x++) {
$year_html .= '<option value='.$x.($selected_year == $x ? ' selected=true' : '' ).'>'.$x.'</option>';
}
$year_html .= '</select>';
// output
print $year_html;
Output:
EDIT
If your source data will be in the format of YYYY-MM-DD. In order to translate that into a timestamp, and thus work with the example code I've posted, is to call strtotime on that formatted date value. Thus, the only line in the code above that would need to be changed is $timestamp = strtotime($the_formatted_date);
<?php
// $months = array(1 => 'January', 2 => 'February', ... etc
?>
<select name="date_mo">
<? for ($i = 1, $current = (int) date('n'); $i <= 12; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$months[$i];?></option>
<? } ?>
</select>
<select name="date_dy">
<? for ($i = 1, $current = (int) date('j'); $i <= 31; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$i;?></option>
<? } ?>
<select name="date_yr">
<? for ($i = $current = (int) date('Y'), $until = $i + 50; $i <= $until; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$i;?></option>
<? } ?>
</select>
I would personally convert the 3 fields to a more user-friendly date selector (using for example jQuery UI), but to have the correct option selected when you open the page, you need to add a check to each option.
Example for the year:
<?php
$selected_year = date("Y", $date_timestamp); // get the selected year
for($year=$curryear;$year<$curryear+51;$year++)
{
?>
<option value="<?php echo $year; ?>" <?php echo ($selected_year == $year) ? 'selected' : ''; ?>><?php echo $year; ?></option>
<?php
}
?>

Categories