Jump to content

Date search used to work now it doesnt?


peat

Recommended Posts

Hi,

Ive used this php code for years after someone here kindly helped me out. But in the last few months its just stopped working.

I know that the login bit works ok so it must be somewhere in this stuff.  Can anyone see something that has caused the problem.

(What happens is when I type for example "18-07" into the date search box and then i hit the execute button nothing happens now whereas before it would spit out a big table of records within 3 days either side of the 18th.)

Thanks in advance.

 

 

<?php
list($day, $month) = explode('-', $query);
$query = '2000-'.$month.'-'.$day;


//the variable query should now have a date formatted for the mysql query

$result = mysql_query("SELECT *, DATE_FORMAT(Date, '%m%d') AS mmdd from Records WHERE DATE_FORMAT(Date, '%m%d') BETWEEN DATE_FORMAT(DATE_SUB('$query', INTERVAL 3 DAY), '%m%d') AND DATE_FORMAT(DATE_ADD('$query', INTERVAL 3 DAY), '%m%d')ORDER BY mmdd", $db) or die(mysql_error());

// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)){

// stores the date in dd-mm format as variable $date2
	$date2 = date('j M', strtotime($row['Date']));

echo "<tr><td>".$row['Bird']."</td><td>".$date2."</td><td>".$row['Location']."</td></tr>";

Link to comment
Share on other sites

my web provider did upgrade to php5 but you have to select that so i am still on php4.

 

I didnt realise that about 2000 huggie but its ok as there are records from 1994 so something should come up.

 

ted - where do I put that code? sorry i really am the worlds worst at php :(

Link to comment
Share on other sites

for eg. if your date is "2007/02/02", and you want the results from "2007/01/30" to "2007/02/05":

<?php
//I assume that your date format is "2007/02/02" without the quotation marks
//then...
$firstdate = strtotime("2007/02/02") - 259200;//you get 2007/01/30 in strtotime form, convert it:
$firstdate = date("Y-m-d", $firstdate);//converted...
//do the same with the second date:
$seconddate = strtotime("2007/02/02") + 259200;//you get 2007/02/05 in strtotime form, convert it:
$seconddate = date("Y-m-d", $seconddate);//converted...

//the variable query should now have a date formatted for the mysql query

$result = mysql_query("SELECT * FROM table BETWEEN '$firstdate' AND '$seconddate'") or die(mysql_error());

// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)){

// stores the date in dd-mm format as variable $date2
	$date2 = date('j M', strtotime($row['Date']));

echo "<tr><td>".$row['Bird']."</td><td>".$date2."</td><td>".$row['Location']."</td></tr>";

I think this would give you what you want.

Ted

Link to comment
Share on other sites

I didnt realise that about 2000 huggie but its ok as there are records from 1994 so something should come up.

 

No they won't!

 

The only records that will show are those from the year 2000!  That's what the hard coded 2000 is doing in this line...

 

$query = '2000-'.$month.'-'.$day;

 

Regards

Huggie

 

 

Link to comment
Share on other sites

sorry, i think this should be the correct format:

<?php
//I assume that your date format is "2007/02/02" without the quotation marks
//then...
$firstdate = strtotime("2007/02/02") - 259200;//you get 2007/01/30 in strtotime form, convert it:
$firstdate = date("Y-m-d", $firstdate);//converted...
//do the same with the second date:
$seconddate = strtotime("2007/02/02") + 259200;//you get 2007/02/05 in strtotime form, convert it:
$seconddate = date("Y-m-d", $seconddate);//converted...

//the variable query should now have a date formatted for the mysql query

$result = mysql_query("SELECT * FROM table WHERE date BETWEEN '$datetime1' AND '$datetime2'") or die(mysql_error());//change table and date to your own table name and column name

// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)){

// stores the date in dd-mm format as variable $date2
	$date2 = date('j M', strtotime($row['Date']));

echo "<tr><td>".$row['Bird']."</td><td>".$date2."</td><td>".$row['Location']."</td></tr>";
?>

Link to comment
Share on other sites

Hi Ted,

It says this now when loading up the page

Parse error: syntax error, unexpected $end in /pages/datesearch.php on line 66

Im confused now cus ive just checked and line 66 has nothing in it and its after all the code html or php.

 

 

 

Link to comment
Share on other sites

that means i forgot to add a } at the end, here:

<?php
//I assume that your date format is "2007/02/02" without the quotation marks
//then...
$firstdate = strtotime("2007/02/02") - 259200;//you get 2007/01/30 in strtotime form, convert it:
$firstdate = date("Y-m-d", $firstdate);//converted...
//do the same with the second date:
$seconddate = strtotime("2007/02/02") + 259200;//you get 2007/02/05 in strtotime form, convert it:
$seconddate = date("Y-m-d", $seconddate);//converted...

//the variable query should now have a date formatted for the mysql query

$result = mysql_query("SELECT * FROM table WHERE date BETWEEN '$datetime1' AND '$datetime2'") or die(mysql_error());//change table and date to your own table name and column name

// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)){

// stores the date in dd-mm format as variable $date2
	$date2 = date('j M', strtotime($row['Date']));

echo "<tr><td>".$row['Bird']."</td><td>".$date2."</td><td>".$row['Location']."</td></tr>";}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.