Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Query Question 1

Status
Not open for further replies.

vetteguy69

Technical User
Feb 22, 2002
13
0
0
US
I have a MySQL database and was looking for a query to take in 2 form variables (beginning and end date) and based on those dates, return users from the database who's birthday(already in database column) is within that date range. How would I achieve this type of functionality?
 
Your select will look something like

&quot;SELECT * FROM mytable WHERE birthdate <= $startdate AND birthdate >= $enddate&quot;

You might also find the following function useful:
/**
*
* Display a form to enable you to select a date
*
* e.g. To show a start_date and end_date form:
* date_selector(&quot;start_&quot;, $start_date);
* date_selector(&quot;end_&quot;, $end_date );
*
*/
function date_selector($inName, $useDate=0)
{
//create array so we can name months
$monthName = array(1=> &quot;Jan&quot;, &quot;Feb&quot;, &quot;Mar&quot;,
&quot;Apr&quot;, &quot;May&quot;, &quot;Jun&quot;, &quot;Jul&quot;, &quot;Aug&quot;,
&quot;Sep&quot;, &quot;Oct&quot;, &quot;Nov&quot;, &quot;Dec&quot;);

//if date invalid or not supplied, use current time
if($useDate == 0) {
$useDate = Time();
}

/*
** make month selector
*/
print(&quot;<select name=&quot; . $inName . &quot;month>\n&quot;);
for($currentMonth = 1; $currentMonth <= 12; $currentMonth++) {
print(&quot;<option value=\&quot;&quot;);
print(intval($currentMonth));
print(&quot;\&quot;&quot;);
if(intval(date( &quot;m&quot;, $useDate))==$currentMonth) {
print(&quot; selected&quot;);
}
print(&quot;>&quot; . $monthName[$currentMonth] . &quot;\n&quot;);
}
print(&quot;</select>&quot;);

/*
** make day selector
*/
print(&quot;<select name=&quot; . $inName . &quot;day>\n&quot;);
for($currentDay=1; $currentDay <= 31; $currentDay++) {
print(&quot;<option value=\&quot;$currentDay\&quot;&quot;);
if(intval(date( &quot;d&quot;, $useDate))==$currentDay) {
print(&quot; selected&quot;);
}
print(&quot;>$currentDay\n&quot;);
}
print(&quot;</select>&quot;);

/*
** make year selector
*/
print(&quot;<select name=&quot; . $inName . &quot;year>\n&quot;);
$startYear = date( &quot;Y&quot;, $useDate);
for($currentYear = $startYear - 5; $currentYear <= $startYear; $currentYear++) {
print(&quot;<option value=\&quot;$currentYear\&quot;&quot;);
if(date( &quot;Y&quot;, $useDate)==$currentYear) {
print(&quot; selected&quot;);
}
print(&quot;>$currentYear\n&quot;);
}
print(&quot;</select>&quot;);
}
 
In trying to use the query you suggested I am running into some problems. When I submit my data from my form to the .php page with the query on it, it doesn't return any results. What data type should the dob field be in my database. I have it set as text now and I think it's wrong. Any suggestions?

--Here's the code I have so far--

-Form Page-
<html>
<head>
<title>Form Page</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;bdayfind.php&quot;>
<p>Start Date
<input type=&quot;text&quot; name=&quot;startdate&quot;>
(mm/dd/yyyy)</p>
<p>End Date
<input type=&quot;text&quot; name=&quot;enddate&quot;>
(mm/dd/yyyy)</p>
<p>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
<input type=&quot;reset&quot; name=&quot;Reset&quot; value=&quot;Reset&quot;>
</p>
</form>
</body>
</html>


--bdayfind.php--
<html>

<head>

<title>Birthday Finder</title>

<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;../style.css&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<center>

<?php

// Connecting, selecting database

$link = mysql_connect(&quot;albanycoors.com&quot;, &quot;username&quot;, &quot;password&quot;)
or die(&quot;Could not connect&quot;);

mysql_select_db(&quot;albanycoor&quot;)
or die(&quot;Could not select database&quot;);

// Performing SQL query

$query = &quot;SELECT * FROM Membership WHERE dob <= $startdate AND dob >= $enddate&quot;;


$result = mysql_query($query)
or die(&quot;Query failed&quot;);

// Printing results in HTML
print &quot;\t<center><table border=1 cellpadding=2 cellspacing=2>\n&quot;;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

print &quot;\t<tr>\n&quot;;

foreach ($line as $col_value) {
print &quot;\t\t<td class=textbold>  $col_value  </td>\n&quot;;
}
print &quot;\t</tr>\n&quot;;
}

print &quot;</table></center>\n&quot;;

// Free resultset
mysql_free_result($result);


// Closing connection
mysql_close($link);
?>

</center>
</body>
</html>




 
$query = &quot;SELECT * FROM Membership WHERE dob <= '$startdate' AND dob >= '$enddate'&quot;;

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

both should help:)
***************************************
Party on, dudes!
 
Vetteguy69,

Firstly, your date field must be in the datatype of date! Don't even think about fudging it with another datatype, please ;).

I have just looked in mysql.com and searched for 'date' and got the following info:

-------------------------------------------------------
The format of a DATE value is 'YYYY-MM-DD'. According to ANSI SQL, no other format is allowed. You should use this format in UPDATE expressions and in the WHERE clause of SELECT statements. For example:

mysql> SELECT * FROM tbl_name WHERE date >= '1997-05-05';
--------------------------------------------------------

So, all you need to do is build your date string in the format 'YYYY-MM-DD' and then you can do the comparisms easily.

Looking at your code, the first thing I would do is blend in the function which I proposed to you, i.e. instead of:

<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;bdayfind.php&quot;>
<p>Start Date
<input type=&quot;text&quot; name=&quot;startdate&quot;>
(mm/dd/yyyy)</p>
<p>End Date
<input type=&quot;text&quot; name=&quot;enddate&quot;>
(mm/dd/yyyy)</p> etc.

use something like:

<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;bdayfind.php&quot;>
<p>Start Date
<?
date_selector(&quot;start_&quot;);
?>
<p>End Date
<?
date_selector(&quot;end_&quot;);
?>

That will give you a very user friendly date selection.

Next you need to process all the bits in the receiving .php file. Basically you just need to join the bits together in the required order, a bit like as follows (in bdayfind.php):

$built_start_date = $start_year . &quot;-&quot; . $start_month . &quot;-&quot; . $start_day;
$built_end_date = $end_year . &quot;-&quot; . $end_month . &quot;-&quot; . $end_day;

Now you can do the SQL comparism as in the SQL documentation above.

Just stick plenty of 'print' statements everywhere to understand what is going on step by step, and I'm sure you'll soon have it licked.

Happy coding!

JavaSprout
 
ok got that working. For my next trick I'm trying to make a dropdown list with the months and based on the month the user selects it displays the people whose bday is in that month. HOw would I do this?
 
The logic to me is:

1) Work out the first day of the month (easy: you know the month!)
2) Work out the first day of the next month (trickier: check php.net date/time functions, I'm sure there's either a function built in or someone's written one)
3) Do the same sort of query you now know how to do now using the start and end dates in (1) and (2) above (being careful of course about your <= or < etc.

:)

Go get 'em Floyd.
 
SELECT * FROM Membership WHERE dob like '____-$mymonth-__';

Months must be given as 01 02 03 .. 12 ***************************************
Party on, dudes!
 
To clarify the above post, in mysql an underscore _ matches any single chracter, put 4 in for the year ____ and a - then the month value then - then you can either finish with % for any other characters or continue with underscores. ***************************************
Party on, dudes!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top