Jump to content

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
https://forums.phpfreaks.com/topic/36750-date-search-used-to-work-now-it-doesnt/
Share on other sites

OK, please tell me that you're fully aware this script is only catering for dates in the year 2000!  So if you're trying to retrieve something that you expect to be outside that year, that's why you're getting no results.

 

Regards

Huggie

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 :(

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

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

 

 

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>";
?>

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>";}
?>

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.