SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Comparing Dates within a Date range

    Hello,

    I have a start date and end date. I want to compare another date within the start date and end date.

    I am usying mysql db and the start date and end date is coming from db and the date which needs to be compared is coming from user. here is the code i am trying which is not working:

    Code:
    $q1 = "SELECT * FROM reservations WHERE '$startd' >= sdate AND '$startd' =< fdate"; 
    $r1 = mysql_query($q1) or die(mysql_error()); 
    $rows = mysql_num_rows($r1);
    Please help me with this.

    Thanks

  2. #2
    SitePoint Evangelist anjanesh's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whats the format of $startd ? If its in some proper format then UNIX_TIMESTAMP() will work for it.
    Code:
    $q1 = "
    SELECT * FROM reservations 
    WHERE 
    UNIX_TIMESTAMP($startd) >= UNIX_TIMESTAMP(sdate) AND
    UNIX_TIMESTAMP($startd) =< UNIX_TIMESTAMP(fdate)
    ";
    $r1 = mysql_query($q1) or die(mysql_error());
    $rows = mysql_num_rows($r1);
    Anjanesh

  3. #3
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The date is not in unix timestamp format. In db also its in date format i.e: 2005-01-31 so my date also is in format of 2005-01-31 i.e: yyyy-mm-dd.

    I will try the code you gave.

    Thanks

  4. #4
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I also tried the query you gave me and its giving error:

    You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '=< UNIX_TIMESTAMP(fdate)' at line 3

    Please help.

    Thanks

  5. #5
    SitePoint Evangelist anjanesh's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $q1 = "
    SELECT * FROM reservations 
    WHERE 
    UNIX_TIMESTAMP($startd) >= UNIX_TIMESTAMP(sdate) AND
    UNIX_TIMESTAMP($startd) <= UNIX_TIMESTAMP(fdate)
    ";
    Anjanesh

  6. #6
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The code does not seems to be working becuase i have start date as 2005-05-20 and enddate as 2005-05-25 and my compare date is: 2005-05-20 and its not showing that it exists so i think the code is not comparing ...dates properly.

    Thanks

  7. #7
    SitePoint Evangelist anjanesh's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whats the value of $startd ? 2005-05-20 ?
    Code:
    $q1 = "
    SELECT * FROM reservations 
    WHERE 
    UNIX_TIMESTAMP('$startd') >= UNIX_TIMESTAMP(sdate) AND
    UNIX_TIMESTAMP('$startd') <= UNIX_TIMESTAMP(fdate)
    ";
    Anjanesh

  8. #8
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    startd has value of : 2005-05-20.

    Thanks

  9. #9
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    556
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks a lot it works muhahahahahaha.

    Thanks again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •