Page 1 of 1
to show results between two customisable dates
Posted: Tue Aug 08, 2006 10:28 am
by FE
hello, i'm having a bit or trouble trying to make a script in php so users can select two date, click submit and then return the results
i need the script to show the choosen date on the returned page
the dates will be used for an MYSQL query
thanks in advance
Posted: Tue Aug 08, 2006 10:34 am
by feyd
What code have you done so far?
Posted: Tue Aug 08, 2006 10:46 am
by FE
Code: Select all
$Current = explode( "-", date( "n-j-Y", strtotime("-180 days") ) ); //start date - default, 6 months
$Current1 = explode( "-", date( "n-j-Y" ) ); //end date
echo '<form method="post" action="scrappedreport.php">';
$Month = $Current[0];
$Day = $Current[1];
$Year = $Current[2];
$FirstYear = 1980;
$LastYear = $Year; // Only display up to the current year
// Output Days
echo "<TR><TD>Day:</TD><TD><SELECT NAME='day'>\n";
for( $i = 1; $i <= 31; $i++ )
{
echo "<OPTION NAME='day' VALUE='" . $i . "'";
if ( $i == $Day ) echo " SELECTED";
echo ">" . $i . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
// Output Months
echo "<TR><TD>Month:</TD><TD><SELECT NAME='month'>\n";
for( $i = 1; $i <= 12; $i++ )
{
echo "<OPTION NAME='month'";
if ( $i == $Month ) echo " SELECTED";
echo ">" . date( "M", mktime( 0, 0, 0, $i, $Day, $Year ) ) . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
// Output Years
echo "<TR><TD>Year:</TD><TD><SELECT NAME='year'>\n";
for( $i = $FirstYear; $i <= $LastYear; $i++ )
{
echo "<OPTION NAME='year' VALUE='" . $i . "'";
if ( $i == $Year ) echo " SELECTED";
echo ">" . $i . "</OPTION>\n";
}
echo "</SELECT></TD</TR><br>\n";
//==============================================================================================================================================
$Month1 = $Current1[0];
$Day1 = $Current1[1];
$Year1 = $Current1[2];
$FirstYear1 = 1980;
$LastYear1 = $Year1; // Only display up to the current year
// Output Days
echo "<TR><TD>Day:</TD><TD><SELECT NAME='day1'>\n";
for( $i1 = 1; $i1 <= 31; $i1++ )
{
echo "<OPTION NAME='day1' VALUE='" . $i1 . "'";
if ( $i1 == $Day1 ) echo " SELECTED";
echo ">" . $i1 . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
// Output Months
echo "<TR><TD>Month:</TD><TD><SELECT NAME='month1'>\n";
for( $i1 = 1; $i1 <= 12; $i1++ )
{
echo "<OPTION NAME='month1'";
if ( $i1 == $Month1 ) echo " SELECTED";
echo ">" . date( "M", mktime( 0, 0, 0, $i1, $Day1, $Year1 ) ) . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
// Output Years
echo "<TR><TD>Year:</TD><TD><SELECT NAME='year1'>\n";
for( $i1 = $FirstYear1; $i1 <= $LastYear1; $i1++ )
{
echo "<OPTION NAME='year1' VALUE='" . $i1 . "'";
if ( $i1 == $Year1 ) echo " SELECTED";
echo ">" . $i1 . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
echo '<br><input type="submit" name="submitted" value="Submit"/><br>';
Code: Select all
if (isset($_POST['submitted'])) {
$errors = array();
$day2=$_POST['day'];
$month2=$_POST['month'];
$year2=$_POST['year'];
$day3=$_POST['day1'];
$month3=$_POST['month1'];
$year3=$_POST['year1'];
// the start year is higher than the end year
if ($year2 > $year3) {
$errors[] = 'The end year you have entered is not correct';
}
//if the years are the same
if ($year2 == $year3 ) {
//and if the start month is equal to or higher than the end date
if ($monthno == $monthno1){
if ($day >= $day1) {
$errors[] = 'Error! The start date can not be on or before the end date!';
}}
elseif ($monthno > $monthno1){
$errors[] = 'Error! The start date can not be before the end date!';
}
}
if (!empty($errors)) { // Print any error messages.
echo '<h2>Error!</h2>
<p class="error"><font color=red>The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo "<b> - $msg<br /></b>\n";
}
echo '</p><p>Please try again.</font></p>';
}
if (empty($errors)) {
....
this is all i've got really, i've tried alot of different methods, the first bit of code is just the two drop down boxes, the default is 180 days
Posted: Tue Aug 08, 2006 10:53 am
by feyd
What are you having trouble with then?
Posted: Tue Aug 08, 2006 10:59 am
by FE
making the script so that users can select two dates
click submit which will the do a search in mysql and return the results,
the major problems i am having are:
getting two php variables which are holding the date varaibles which i can use for the MYSQL query
e.g
$startdate and $enddate
and showing the choosen $startdate and $enddate in the new page in the drop down boxes and not the default (-180 days)
Posted: Wed Aug 09, 2006 3:24 am
by FE
can anyone help? can anyone point me to a script? i'm pretty sure this kinda script has been done before
does any know how to customise a date, but not using echo?
e.g 'date (5-jan-06);'
thank in advance
Posted: Wed Aug 09, 2006 3:53 am
by RobertGonzalez
Do you have to use dropdowns, or can you just let your users enter a date by hand? As for using
date() to format a date, the manual describes what you can do with it pretty well. The
date() function returns the formatted version of the timestamp given to it, or if there is no timestamp, the current unix time (also gathered by using the
time() function).
Posted: Wed Aug 09, 2006 4:03 am
by FE
i suppose if i have no other option or for a temp solution, it should be ok
i think i have found a solution but using this code i found:
Code: Select all
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}
$date1="07/11/2003";
$date2="09/04/2004";
print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";
still working on the code right now
Posted: Wed Aug 09, 2006 4:18 am
by RobertGonzalez
Say I the user choose 3/16/2006 and 6/27/2006. What is your script going to do with those two dates?
Posted: Wed Aug 09, 2006 4:35 am
by FE
i want my script to take the two dates,
1. put it into 2 variables,
2. do some validation check to make sure the start date isn't before the end date (currently using the code below)
Code: Select all
// the start year is higher than the end year
if ($year2 > $year3) {
$errors[] = 'The end year you have entered is not correct';
}
//if the years are the same
if ($year2 == $year3 ) {
//and if the start month is equal to or higher than the end date
if ($monthno == $monthno1){
if ($day >= $day1) {
$errors[] = 'Error! The start date can not be on or before the end date!';
}}
elseif ($monthno > $monthno1){
$errors[] = 'Error! The start date can not be before the end date!';
}
}
if (!empty($errors)) { // Print any error messages.
echo '<h2>Error!</h2>
<p class="error"><font color=red>The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo "<b> - $msg<br /></b>\n";
}
echo '</p><p>Please try again.</font></p>';
}
3. use the two dates for an mysql search
Code: Select all
$query = "SELECT batchno, tested_date, tested_by, notes, account_ref, date_rec FROM batch WHERE date_rec BETWEEN '$startdate' and '$enddate' AND status = 3";
$result = @mysql_query ($query); // Run the query.
4. on the new page show the users choosen date '3/16/2006' and '6/27/2006' in the drop down boxes
i've seen this function on online banks where you can choose 2 dates to view transactions etc
Aslo i dont know if anyone can help but this code
Code: Select all
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}
$date21=$day2 . '/' . $monthno . '/' . $year2;
$date11=$day3 . '/' . $monthno1 . '/' . $year3;
print "If we minus " . $date11 . " from " . $date21 . " we get " . dateDiff("/", $date21, $date11) . ".";
which i posted earlier doesn't seem to be able to work out the days between dates which are within the year for example:
"If we minus 9/08/2006 from 10/02/2006 we get 24."
really weird but if i do more than a years gap it works e.g.
"If we minus 9/08/2006 from 10/02/2005 we get 341."
thanks again guys
Posted: Wed Aug 09, 2006 7:42 am
by FE
alright guys
i think i've solved it, i've just got one little problem which i can't seem to get over
this code works (using a variable to use custom dates)
Code: Select all
$startdate = date("Y-m-d", strtotime("$startdate1 days"));
$enddate = date("Y-m-d", strtotime("$enddate1 days"));
this code is where i am having trouble with, when i do echo $current and $current1 i get "Array", i've tried using the above method but it doens't return anything, i'm guessing its because the drop down boxes uses an array, anyone help?
i know the error is because i'm using $startdate1. it works with "-180" but i need it to work with a variable
anyone with a bit more knowledge with explode help me here?
Doesn't work - tried with and without ' ' around $startdate and $enddate e.g. '$startdate'
Code: Select all
$Current = explode( "-", date( "n-j-Y", strtotime("$startdate1 days") ) ); //start date - default, 6 months
$Current1 = explode( "-", date( "n-j-Y", strtotime("$enddate1 days") ) ); //end date
//the array
$Month = $Current[0];
$Day = $Current[1];
$Year = $Current[2];
Works
Code: Select all
$Current = explode( "-", date( "n-j-Y", strtotime("-180 days") ) ); //start date - default, 6 months
$Current1 = explode( "-", date( "n-j-Y" ) ); //end date
$Month = $Current[0];
$Day = $Current[1];
$Year = $Current[2];
cheers guys, i will post the finished code for future reference
Posted: Wed Aug 09, 2006 8:07 am
by FE
ah i've done it guys heres the code, yeah i know its not the best but hey it works, any improvements would be great
Code: Select all
if (PHP_VERSION >= '5.1.0RC1')
{
date_default_timezone_set('Europe/London');
}
//default dates
$Current = explode( "-", date( "n-j-Y", strtotime("-180 days") ) ); //start date - default, 6 months
$Current1 = explode( "-", date( "n-j-Y" ) ); //end date
$startdate = date("Y-m-d", strtotime("-180 days"));
$enddate = date("Y-m-d");
//echo 'enddate ' . $enddate . '<br>';
//echo 'startdate ' . $startdate . '<br>';
$Month = $Current[0];
$Day = $Current[1];
$Year = $Current[2];
$Month1 = $Current1[0];
$Day1 = $Current1[1];
$Year1 = $Current1[2];
if (isset($_POST['submitted'])) {
$errors = array();
$day2=$_POST['day'];
$month2=$_POST['month'];
$year2=$_POST['year'];
$day3=$_POST['day1'];
$month3=$_POST['month1'];
$year3=$_POST['year1'];
if ($month2 == Jan){
$monthno = '01';
} elseif ($month2 == Feb){
$monthno = '02';
} elseif ($month2 == Mar){
$monthno = '03';
} elseif ($month2 == Apr){
$monthno = '04';
} elseif ($month2 == May){
$monthno = '05';
} elseif ($month2 == Jun){
$monthno = '06';
} elseif ($month2 == Jul){
$monthno = '07';
} elseif ($month2 == Aug){
$monthno = '08';
} elseif ($month2 == Sep){
$monthno = '09';
} elseif ($month2 == Oct){
$monthno = '10';
} elseif ($month2 == Nov){
$monthno = '11';
} elseif ($month2 == Nov){
$monthno = '12';
}
if ($month3 == Jan){
$monthno1 = '01';
} elseif ($month3 == Feb){
$monthno1 = '02';
} elseif ($month3 == Mar){
$monthno1 = '03';
} elseif ($month3 == Apr){
$monthno1 = '04';
} elseif ($month3 == May){
$monthno1 = '05';
} elseif ($month3 == Jun){
$monthno1 = '06';
} elseif ($month3 == Jul){
$monthno1 = '07';
} elseif ($month3 == Aug){
$monthno1 = '08';
} elseif ($month3 == Sep){
$monthno1 = '09';
} elseif ($month3 == Oct){
$monthno1 = '10';
} elseif ($month3 == Nov){
$monthno1 = '11';
} elseif ($month3 == Nov){
$monthno1 = '12';
}
// the start year is higher than the end year
if ($year2 > $year3) {
$errors[] = 'The end year you have entered is not correct';
}
//if the years are the same
if ($year2 == $year3 ) {
//and if the start month is equal to or higher than the end date
if ($monthno == $monthno1){
if ($day2 >= $day3) {
$errors[] = 'Error! The start date can not be on or before the end date!';
}}
elseif ($monthno > $monthno1){
$errors[] = 'Error! The start date can not be before the end date!';
}
}
if (!empty($errors)) { // Print any error messages.
echo '<h2>Error!</h2>
<p class="error"><font color=red>The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo "<b> - $msg<br /></b>\n";
}
echo '</p><p>Please try again.</font></p>';
}
if (empty($errors)) {
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}
//store the date for the end date (take the current date away from the choosen date)
//month-day-year
$date1=$monthno1 . '/' . $day3 . '/' . $year3;
//echo '1st ' . $date1 . '<br>';
$date2=date( "m/d/Y" );
//echo '2nd ' . dateDiff("/", $date2, $date1) . '<br>';
$startdate1 = dateDiff("/", $date2, $date1);
//find the start date
$date21=$monthno . '/' . $day2 . '/' . $year2;
$date11=date( "m/d/Y" );
//echo '3rd ' . dateDiff("/", $date21, $date11) . '<br>';
$enddate1 = dateDiff("/", $date21, $date11);
$enddate = date("Y-m-d", strtotime("$startdate1 days"));
$startdate = date("Y-m-d", strtotime("$enddate1 days"));
//echo 'enddate1 ' . $enddate1 . '<br>';
//echo 'enddate ' . $enddate . '<br>';
//echo 'startdate ' . $startdate . '<br>';
$Month = $monthno;
$Day = $day2;
$Year = $year2;
$Month1 = $monthno1;
$Day1 = $day3;
$Year1 = $year3;
require_once ('../mysql_connect.php');
mysql_select_db("batch");
$query = "SELECT batchno, tested_date, tested_by, notes, account_ref, date_rec FROM batch WHERE date_rec BETWEEN '$startdate' and '$enddate' AND status = 3";
$result = @mysql_query ($query); // Run the query.
}//end of if no erros statement
}// end of submitted
echo '<form method="post" action="scrappedreport.php">';
$FirstYear = 1980;
$LastYear = $Year; // Only display up to the current year - Change if you need
// Output Days
echo "<TR><TD>Day:</TD><TD><SELECT NAME='day'>\n";
for( $i = 1; $i <= 31; $i++ )
{
echo "<OPTION NAME='day' VALUE='" . $i . "'";
if ( $i == $Day ) echo " SELECTED";
echo ">" . $i . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
// Output Months
echo "<TR><TD>Month:</TD><TD><SELECT NAME='month'>\n";
for( $i = 1; $i <= 12; $i++ )
{
echo "<OPTION NAME='month'";
if ( $i == $Month ) echo " SELECTED";
echo ">" . date( "M", mktime( 0, 0, 0, $i, $Day, $Year ) ) . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
// Output Years
echo "<TR><TD>Year:</TD><TD><SELECT NAME='year'>\n";
for( $i = $FirstYear; $i <= $LastYear; $i++ )
{
echo "<OPTION NAME='year' VALUE='" . $i . "'";
if ( $i == $Year ) echo " SELECTED";
echo ">" . $i . "</OPTION>\n";
}
echo "</SELECT></TD</TR><br>\n";
//===============================================================================
$FirstYear1 = 1980;
$LastYear1 = $Year1; // Only display up to the current year - Change if you need
// Output Days
echo "<TR><TD>Day:</TD><TD><SELECT NAME='day1'>\n";
for( $i1 = 1; $i1 <= 31; $i1++ )
{
echo "<OPTION NAME='day1' VALUE='" . $i1 . "'";
if ( $i1 == $Day1 ) echo " SELECTED";
echo ">" . $i1 . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
// Output Months
echo "<TR><TD>Month:</TD><TD><SELECT NAME='month1'>\n";
for( $i1 = 1; $i1 <= 12; $i1++ )
{
echo "<OPTION NAME='month1'";
if ( $i1 == $Month1 ) echo " SELECTED";
echo ">" . date( "M", mktime( 0, 0, 0, $i1, $Day1, $Year1 ) ) . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
// Output Years
echo "<TR><TD>Year:</TD><TD><SELECT NAME='year1'>\n";
for( $i1 = $FirstYear1; $i1 <= $LastYear1; $i1++ )
{
echo "<OPTION NAME='year1' VALUE='" . $i1 . "'";
if ( $i1 == $Year1 ) echo " SELECTED";
echo ">" . $i1 . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";
echo '<br><input type="submit" name="submitted" value="Submit"/><br>';
echo '</form>'; // end of select date form
Posted: Wed Aug 09, 2006 8:40 am
by JayBird
Instead of this (which by the way has an error in it at the end, you cjeck for "nov" twice)
Code: Select all
if ($month2 == Jan){
$monthno = '01';
} elseif ($month2 == Feb){
$monthno = '02';
} elseif ($month2 == Mar){
$monthno = '03';
} elseif ($month2 == Apr){
$monthno = '04';
} elseif ($month2 == May){
$monthno = '05';
} elseif ($month2 == Jun){
$monthno = '06';
} elseif ($month2 == Jul){
$monthno = '07';
} elseif ($month2 == Aug){
$monthno = '08';
} elseif ($month2 == Sep){
$monthno = '09';
} elseif ($month2 == Oct){
$monthno = '10';
} elseif ($month2 == Nov){
$monthno = '11';
} elseif ($month2 == Nov){
$monthno = '12';
}
You could do something along the lines of
Code: Select all
$months = array(NULL, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$monthno = array_search($month2, $months);