0

I have the following table (dates) where columns are in date format as defined.

id   Column1                Column2       
 1   [2013-07-12 12:00:00]  [2013-07-14 12:00:00]   

I want to extract the range like this(the dates between column1 and column2):

2013-07-12 12:00:00

2013-07-13 12:00:00

2013-07-14 12:00:00

How to make it possible?

3
  • What do you want the result of the SQL query to be? The $result line doesn't make any sense. Are you expecting 3 columns for that 1 record or is that a string? Commented Nov 11, 2013 at 22:05
  • Im expecting 3 culumns, the 3 dates between 07/12 and 07/14. I hope i'm clear. Commented Nov 11, 2013 at 22:11
  • It is nearly impossible to create columns dynamically. But now it looks like you changed your question such that it implies you want rows for each value (which, while easier than columns, is still complicated.) You may want to focus on a PHP centric solution. Commented Nov 11, 2013 at 22:27

3 Answers 3

3

Try this one:

SELECT * FROM DateTable
WHERE DATEDIFF('2013-07-14',dateFROM)>=0
AND DATEDIFF('2013-07-14', dateTO) <= 0

See my SQLFiddle Demo

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciated your help this will help
0

Here is PHP script that will take the two dates returned from your table, separate each day, and then put that day into an array. The dates can then be easily accessed from inside the array.

<?php
    $date_1 = date("Y-m-d g:i:s", strtotime('2013-06-27 12:00:00'));
    $date_2 = date("Y-m-d g:i:s", strtotime('2013-07-03 12:00:00'));
    $results = array($date_1);
    $i = $date_1;
    while ($i <= $date_2) {
        $i = date("Y-m-d g:i:s", strtotime("+1 day", strtotime($i)));
        array_push($results, $i);
        echo $i;
    }
    print_r($results);  
?>

$date_1 and $date_2 will be the values returned from the table. Be sure you assign the earliest date to $date_1 or the script will run forever.

1 Comment

this is exactly what i was looking for to reach my scope. Thank you very much.
0

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Maybe do a select * where the datediff between the two columns is less than or equal to your intended range?

1 Comment

this datediff looks more or less what i'm looking for..give me a time to give you a feedback. thanks..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.