Jump to content

[SOLVED] Query for most recent "dates" from todays date


Recommended Posts

Hey

 

I'm making a movie news site and on my home page I'm currently trying to show a short list of the next movies that are due to be released but i don't seem to be getting anywere. i either end up getting nothing back or just the list of all the movie even if they have already been release as show in this link

 

http://bulletsnoctane.co.uk/index.php

 

<h3>Coming Soon..</h3>
<?php

    $sql = "SELECT movie.id, movie.name, movie.released
           FROM movie
           WHERE released >=" . date("Y-m-d") . "
           ORDER BY released DESC
	   LIMIT 0, 10 ";
    $result = mysql_query($sql, $conn)
      or die('Could not get list of movies; ' . mysql_error());
    
    if (mysql_num_rows($result) == 0) {
      echo "  <em>No movies available</em>";
    } else {


      while ($row = mysql_fetch_array($result)) {
			$date1 = $row['released'];
		$date2 = explode("-",date("Y-m-d",strtotime($date1)));//splits up the date

        echo '<a href="/movies/movie.php?id=' .
             $row['id'] . '">' . htmlspecialchars($row['name']) .
             "</a> | ";
	echo $date2[2] . "/" . $date2[1] . "/" . $date2[0];
	echo "<br />";
        
      }
    }
    ?>

Looking at your page, your dates are being showing like

 

The Avengers | 15/07/2011
date("d-m-Y");

 

But your trying to get equals or greater than

date("Y-m-d");

 

Trying changing this

WHERE released >=" . date("Y-m-d") . "

 

to this

WHERE released >=" . date("d-m-Y") . "

Not look sorry the dates are stored as date("Y-m-d") in the database then when you scroll down and explode the and change it into how it is show on the page

 

      while ($row = mysql_fetch_array($result)) {
			$date1 = $row['released'];
		$date2 = explode("-",date("Y-m-d",strtotime($date1)));//splits up the date

        echo '<a href="/movies/movie.php?id=' .
             $row['id'] . '">' . htmlspecialchars($row['name']) .
             "</a> | ";
	echo $date2[2] . "/" . $date2[1] . "/" . $date2[0];
	echo "<br />";
        
      }
    }

If you plug your query into your mysql console or PHPmyadmin, do you get the expected results?

 

It's probably something to do with your column type I'm guessing... Are you using a DATE or DATETIME column type for released?

ive just tried the below query in phpmyadmin and got the same result the data type im using for released is date

 

SELECT movie.id, movie.name, movie.released
FROM movie
WHERE released >=2009 -01 -10
ORDER BY released DESC 
LIMIT 0 , 10 

ive just tried the below query in phpmyadmin and got the same result the data type im using for released is date

 

SELECT movie.id, movie.name, movie.released
FROM movie
WHERE released >=2009 -01 -10
ORDER BY released DESC 
LIMIT 0 , 10 

 

 

Chances are, MySQL would parse that as 2009-1-10.  Or 1998.  Lol.

 

 

You would need to put it in quotes.

 

 

 

Or, you could use curdate() like xtrololis said.

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.