Solved

How do you create a drop down list of dates

Posted on 2008-06-22
9
740 Views
Last Modified: 2013-12-12
Hi,
I would like to generate some php code that generates a drop down list of dates.
Can anyone provide some code for this.

For example I would like it to list say from June 2006, until present....how do you generate some code for this.

Also, if you select a date, like June 2006, how do you write a search code to search a database that has the dates entered as YYYY-MM-DD?

0
Comment
Question by:Amanda Watson
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

by:ob1_
ID: 21842768
To re-display the date you use the date() function. So if your current YYYY-MM-DD date is stored in $fulldate you would do this:

$displaydate = date("F-Y",$fulldate);

This will change the date into "June 2006" format and store it in $displaydate.

To create a drop-down list you have to embed the html option element in a php while loop, that pulls the dates from a table you populate with dates.





$result = mysql_query("SELECT date from datetable");
 

while ($row = mysql_fetch_array($result)) { ?>

    $displaydate = $row["date"];

    <option value="<?php echo $displaydate ;?>"><?php echo $displaydate; ?></option><?php

	} 

Open in new window

0
 
LVL 11

Author Comment

by:Amanda Watson
ID: 21842864
Hi,
I use this code attached

I get this output

$displaydate = $row["StartDate"]; January-1970

The date format is:

2008-01-03

So I am not sure what happened there...something is wrong?

<?php

include("Settings.php"); //Path to Settings.php
 

$displaydate = date("F-Y",$StartDate);

 

$result = mysql_query("SELECT StartDate from event");

 

while ($row = mysql_fetch_array($result)) { ?>

    $displaydate = $row["StartDate"];

    <option value="<?php echo $displaydate ;?>"><?php echo $displaydate; ?></option><?php

        } 

 

?>

 

Open in new window

0
 
LVL 6

Expert Comment

by:ob1_
ID: 21842921
change

$displaydate = $row["StartDate"];

to

$displaydate = date("F-Y",$row['StartDate']);

you have to use the date function to turn the date into a valid "DateTime" object. Also, you can get rid of this line since you are pulling $displaydate from the table:

$displaydate = date("F-Y",$StartDate);

0
 
LVL 6

Expert Comment

by:ob1_
ID: 21842928
Also maybe I should have been more clear - you do have a table called "event" in a SQL database that you have created and linked to earlier in your code right?
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 11

Author Comment

by:Amanda Watson
ID: 21842962
Yes, I  have
0
 
LVL 11

Author Comment

by:Amanda Watson
ID: 21842986
Sorry,
I didn't understand your previous instructions.
can you outline the line number you are referring to please.
I tried a few different options but the date came back as January 1970 from 2008-01-03?
I have changed everything back to what I had posted before.

If I need to change something that you mentioned, please write the line numbers as I didn't know what youare talking about .

Am to confirm, yes the table is event.

Thanks,
0
 
LVL 6

Expert Comment

by:ob1_
ID: 21842990
sorry i was being lazy and did not test it out, there were a few mistakes in the earlier code snippet,
please use this corrected code, i have just tested it. you have to turn the date into a unix timestamp first to work with it using mktime, also i left out the "select name" command to initiate your drop down list. sorry for the confusion.


$result = mysql_query("SELECT StartDate from event");

 

while ($row = mysql_fetch_array($result)) {

    $date_pieces = explode("-",$row["StartDate"]);

	$tstamp = mktime(0,0,0,$date_pieces[1],$date_pieces[2],$date_pieces[0]);

	$displaydate = date("F-Y",$tstamp); 

	?>

    <select name="datelist">

    <option value="<?php echo $displaydate ;?>"><?php echo $displaydate; ?></option><?php

        } 

	?>

Open in new window

0
 
LVL 11

Author Comment

by:Amanda Watson
ID: 21843009
Thats awesome.
Thanks heaps for that!!
Well done!!
0
 
LVL 6

Accepted Solution

by:
ob1_ earned 125 total points
ID: 21843031
You're very welcome. If the code works please mark the thread as solved and assign points for the solution.


0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php call to a non-object 3 31
WordPress TK Title 8 23
form button worked now it doesnt anymore 9 36
mysql joining from the same table 6 29
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now