Jump to content

[SOLVED] Date range in MYSQL query


tejama

Recommended Posts

Hi everyone,

 

I'm trying to extract data in a certain range of date (say 1 month's worth), but my results aren't what I'm expecting.  Can you guys have a look at my code and suggest what may not be coded properly?

 

function display_archive($month) {
  $tempdate = $month. " 1, 2007";
  $startdate = date("F d, Y", strtotime($tempdate));

  $tempdate2 = $month. " 30, 2007";
  $enddate = date("F d, Y", strtotime($tempdate2));

  db_connect();
  $result = mysql_query("select * from blog where entered BETWEEN '$date' and '$enddate'");
  $num_rows = mysql_num_rows($result);
}

Link to comment
Share on other sites

I might be off on this, but I wrote a similar script a while back and here are some snippets from it.  Keep in mind this was talking to ODBC (VFP) and not MySQL so syntax might be wrong but the method will work:

$today="{d '".date('Y-m-d')."'}";
$ninetyDay="{d '".date('Y-m-d', strtotime('-120 days'))."'}";


$connect = odbc_connect("xxxx", "xxxx", "xxxx");
$query = "SELECT itdatshta.`it_date`, itdatshta.`insptype`, itdatshta.`process`, itdatshta.`product`, itdatshta.`descr`,
itdatshta.`rev_level`, itdatshta.`serialno`, itdatshta.`failed`
FROM `itdatshta` itdatshta WHERE
    itdatshta.`it_date` >= $ninetyDay AND
    itdatshta.`it_date` <= $today
    AND
    process = 'Functional Test'";

Link to comment
Share on other sites

function display_archive($month) {
  $tempdate = $month. " 1, 2007";
  $startdate = date("Y-m-d", strtotime($tempdate));

  $tempdate2 = $month. " 30, 2007";
  $enddate = date("Y-m-d", strtotime($tempdate2));

  db_connect();
  $result = mysql_query("select * from blog where entered BETWEEN '$date' and '$enddate'");
  $num_rows = mysql_num_rows($result);
}

Link to comment
Share on other sites

I actually figured it out, here is the code I used:

 

function archive($month) {
  $tempdate = $month. " 1, 2007";
  $startdate = date("Y-m-d H:i:s", strtotime($tempdate));


  $tempdate2 = $month. " 11, 2007";
  $enddate = date("Y-m-d H:i:s", strtotime($tempdate2));

  db_connect();
  $result = mysql_query("select * from blog where entered >= '$startdate' and entered <= '$enddate'");
  $num_rows = mysql_num_rows($result);
}

 

Thanks for the help though!

Link to comment
Share on other sites

Nope, what fixed it was changing the date declaration from:

 

  $startdate = date("F d, Y", strtotime($tempdate));

 

to:

 

  $startdate = date("Y-m-d H:i:s", strtotime($tempdate));

 

for both $startdate and $enddate.

 

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.