Printing Out Last Year's Dates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Printing Out Last Year's Dates

Post by icesolid »

The code below prints out all of the dates one year from today in a selection box. It works great, however now on another spot on my site I want to print out all of the dates one year past from today, kind of reverse the code below.

Code: Select all

<select name="end_date">
<option value="">------ Select A Date ------</option>
<?php
$start = time();
$time = $start;
$end = strtotime("1 year", $start);

do {
    echo "<option value=\"" . date("Y-m-d", $time) . "\">" . date("m-d-Y", $time) . "</option>\n";
    $time = strtotime("1 day", $time);
}

while($time < $end); 
?>                    
</select>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you tried something like this. I haven't, just thought I'd throw it out there...

Code: Select all

<select name="end_date">
<option value="">------ Select A Date ------</option>
<?php
$start = strtotime("-1 year", time());
$time = $start;
$end = time();

do {
    echo "<option value=\"" . date("Y-m-d", $time) . "\">" . date("m-d-Y", $time) . "</option>\n";
    $time = strtotime("1 day", $time);
}

while($time < $end); 
?>                    
</select>
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

Yes I have, strtotime(); does not seem to work as easy as it would appear.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<select name="end_date">
<option value="">------ Select A Date ------</option>
<?php
$start = time() - 31536000; //Today less one year
$time = $start;
$end = time();

do {
    echo "<option value=\"" . date("Y-m-d", $time) . "\">" . date("m-d-Y", $time) . "</option>\n";
    $time = strtotime("1 day", $time);
}

while($time < $end); 
?>                    
</select>
OK, how about if you just subtract a year's worth of seconds and start a year ago?
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

Works great, one question left. How would I make that today date dynamic, like tomorrow it will be a different date and I want this to change by it's self.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thats a sick script. I just tried it out. Very cool!

I didnt know that was possible.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

Yeah, it is very useful when searching a database by start and end dates.

How would I make the start dynamic to one year past todays date?
Last edited by icesolid on Mon Mar 13, 2006 6:56 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You could use a for loop...

Code: Select all

<select name="end_date">
<option value="">------ Select A Date ------</option>
<?php 
$end = time(); 
$start = $end - 31536000; //Today less one year

for ($i = $start; $i < $end; $i += 86400)
{
    echo "<option value=\"" . date("Y-m-d", $i) . "\">" . date("m-d-Y", $i) . "</option>\n";
}
?>                    
</select>
Post Reply