Jump to content

[SOLVED] SQL statement


avo

Recommended Posts

Hi All

 

if i have lots of dates in a sql table formated like 2008-10-01

 

How would i write a sql function please to allow me to get the closest date to NOW() or if there is one the date in the past and forget the future date closest to NOW()

 

Thanks in advance.

 

Link to comment
Share on other sites

Think you might need 2 queries

 

function functionName ($now) {
    //get last date prior to now
    $query1 = mysql_query("SELECT field_date FROM table WHERE field_date < '$now' ORDER BY field_date DESC LIMIT 1");
    $result1 = mysql_fetch_array($result1);
    // get next future date
    $query2 = mysql_query("SELECT field_date FROM table WHERE field_date > '$now' ORDER BY field_date ASC LIMIT 1");
    $result2 = mysql_fetch_array($result2);

    // do a calculation to find which of $result1[field_date] and $result2[field_date] is closest to $now
}

 

Be interested to know how you get on!

HTH

Link to comment
Share on other sites

Hi

 

This looks like it is working well can anyone just have a quick look to see if im missing anything

You was correct i did need two statements.

 

This is what i ended up with

//check to see if there any over due dates
$date_query = mysql_query ("SELECT cal_due FROM serials WHERE cal_due < NOW() ORDER BY cal_due ASC LIMIT 1") or die (mysql_error());
$cal_due=mysql_fetch_array($date_query) ;

if(empty($cal_due['cal_due']))
{
//if not do the future dates and select the closest to NOW()
$date_query = mysql_query ("SELECT cal_due FROM serials WHERE cal_due >= NOW() ORDER BY cal_due ASC LIMIT 1") or die (mysql_error());
$cal_due=mysql_fetch_array($date_query) ;
}

		echo $cal_due['cal_due'] ;
[code/]

Link to comment
Share on other sites

Better off with something like....

 

<?php

function functionName($now) {
  if ($result = mysql_query("SELECT field_date FROM table WHERE field_date < '$now' ORDER BY field_date DESC LIMIT 1")) {
    if (!mysql_num_rows($result)) {
      if ($result = mysql_query("SELECT field_date FROM table WHERE field_date > '$now' ORDER BY field_date ASC LIMIT 1")) {
        if (!mysql_num_rows($result)) {
          return false;
        }
      }
    }
  }
  $row = mysql_fetch_assoc($result);
  return $row['field_date'];
}

?>

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.