Jump to content

[SOLVED] Select data from a date range


nbarone

Recommended Posts

I have some data I want to query between dates. In the MySql database, they have a datetime field (Y-m-d h:i:s)

 

How can I effectivily query a range of date

like: SELECT * FROM table WHERE tb_date < "2009-01-01" AND tb_date > "2009-06-01"

 

Link to comment
Share on other sites

here's what I got:

<?php
function getNumRows($qry){
		if($qry <> NULL){
			$qry = "SELECT * FROM epostcard WHERE ".$qry;
		} else {
			$qry = "SELECT * FROM epostcard";
		}
   		$result = mysql_query($qry);
		$numRows = mysql_num_rows($result);
		unset($qry,$result);
		return $numRows;
	}

$totalPostcards = getNumRows("p_pcSentDate <= '". $dateStart . "' AND p_pcSentDate >= '" . $dateEnd . "'");

 

this returns 0 rows. (incorrect)

 

$totalPostcards = getNumRows("p_pcSentDate BETWEEN '". $dateStart . "' AND '" . $dateEnd . "'");

 

this returns the correct rows, however I need to get the rows that are on dateStart and dateEnd - this does the rows BETWEEN (obv),

Link to comment
Share on other sites

You are still doing <= START and >= END. The date can't be both before a start date and after an end date. you want after a start date and before and end date:

$totalPostcards = getNumRows("p_pcSentDate >= '". $dateStart . "' AND p_pcSentDate <= '" . $dateEnd . "'");

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.