Page 1 of 1
Print Out All Dates In A Year
Posted: Wed Feb 15, 2006 4:26 pm
by icesolid
I am not sure how to print out all of the
dates in a year.
I want the dates to be printed out in this format date("m-d-Y"); and I want them printed out into a selection box like so:
Code: Select all
<select name="ecd">
<option value="$date_here">$date_here</option>
<option value="$next_date_here">$next_date_here</option>
</select>
Help please!
Posted: Wed Feb 15, 2006 4:39 pm
by jayshields
Well, this can be done in many ways, be more specific about your needs.
Do you want the date to go from today to the date before todays in a years time?
Do you want the date to go from 01/01/xx to 31/12/xx?
You could just presume there's 31 days in all months and do some simple loops, but that's a poor man's choice.
If you do this properly, working out the days in February is going to be tricky, and I'm not sure how to do it. I'm sure you could Google along the lines of 'Working out the amount of days in February PHP'. Besides, I'm sure someone will follow me up and tell you just how to work out the amount of days in February given the year using PHP.
If you are designing an interface for something like taking a booking then this is a bad way to let the user choose a date, 365 options in a select box is far too much. A seperate select box for day, month and year is a good way to let the user choose a specific date, possibly in year, month, day order so that the day options can be populated upon the choice of month/year. Ofcourse there are fancy ways to let the user choose a date, like a calendar choice.
Posted: Wed Feb 15, 2006 4:42 pm
by feyd
Code: Select all
$start = time();
$time = $start;
$end = strtotime('+1 year',$start);
do
{
echo date('m-d-Y',$time) . "\n";
$time = strtotime('+1 day',$time);
}
while($time < $end);
untested
Works Like A Charm
Posted: Wed Feb 15, 2006 5:04 pm
by icesolid
Thanks feyd that is some pretty handy code.
Heres my final code:
Code: Select all
<select name="ecd" class="fields">
<option value="">-- Select A Date --</option>
<?php
$start = time();
$time = $start;
$end = strtotime("1 year", $start);
do {
echo "<option value=\"" . date("m-d-Y", $time) . "\">" . date("m-d-Y", $time) . "</option>\n";
$time = strtotime(" 1 day", $time);
}
while($time < $end);
?>
</select>
I have insurance inspectors login and change estimated completion dates, well they are sloppy to say the least so I just wanted a clean way for them to select the day that they are changing the ECD to. After this I have them hit a button and the program updates this date in the database.
Posted: Wed Feb 15, 2006 5:09 pm
by feyd
sorry.. the pluses got removed.. fixed.