Jump to content

List of dates, between two dates - MySQL

- - - - -

  • Please log in to reply
2 replies to this topic

#1
charsta

charsta

    Newbie

  • Members
  • PipPip
  • 28 posts
Hello,

I have a table that among other details it's store a start date and an end date. (Start date is "smaller" than end date!)
Is is possible to print out the dates that are in between start date and end date?
I am using MySQL (and i will try later to import this into php)

e.g.
Start date: 2012-01-01
End date: 2012-01-07

result
2012-01-01
2012-01-02
2012-01-03
2012-01-04
2012-01-05
2012-01-06
2012-01-07

Thanks in advance!

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I believe comparison operators (greater than and less than) work correctly with MySQL date types, with greater than being further into the future and less than being further into the past, so comparing them just like integers should work.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
charsta

charsta

    Newbie

  • Members
  • PipPip
  • 28 posts
This is how I did it in PHP
$query4 has as a result the starting date and
$query5 the end date.


$now = $query4;

while($now < $query5) {

  echo($now . '<br />');

  $now = date('Y-m-d', strtotime(date("Y-m-d", strtotime($now)) . " +1 day"));

}


I want to thank Vaielab that help me on this one!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users