2 Column Mysql Date Range Search in PHP

前端 未结 5 580
悲哀的现实
悲哀的现实 2020-12-18 14:57

MySQL column > sdate, edate ( its 2 column).

sdate is start date for project starting and edate is end date for project ending.

so i need to make search betw

相关标签:
5条回答
  • 2020-12-18 15:33

    As an aside, to help with UI, I recommend using phps strtotime() method... it makes for entering dates very flexible

    0 讨论(0)
  • 2020-12-18 15:33

    SELECT project_name, sdate, edate FROM projects WHERE sdate <= $_POST['edate'] AND edate >= $_POST['sdate']

    Gives you any project with start date and end date that overlap the Form start date and end date. (assuming the form sdate and edate are in the right format)

    0 讨论(0)
  • 2020-12-18 15:39

    assuming that your sdate and edate are of MySQL columns type DATE you could do the following:

    SELECT  
      Project_Name
    , sdate
    , edate
    FROM your_table 
    WHERE 
      sdate <= '2008-12-26'
     AND 
      edate >= '2008-12-26'
    

    or you could use DATEDIFF

    SELECT  
      Project_Name
    , sdate
    , edate
    FROM your_table 
    WHERE 
      DATEDIFF(sdate, '2008-12-26') <= 0
     AND 
      DATEDIFF(edate, '2008-12-26') >= 0
    

    The first one is more efficient because MySQL can compare all the rows in your table to a static value. For the second solution it needs to calculate the difference for every row in your table.

    If your sdate and edate columns are not DATE columns, you are out of luck and need to change them first.

    0 讨论(0)
  • 2020-12-18 15:42

    It seems like a simple select query with a "where" clause can do the trick.

    Peuso-code:

    select sdate, name, edate 
    from your_table 
    where sdate >= '22 December 2008' and edate <= '30 December 2008'
    
    0 讨论(0)
  • 2020-12-18 15:46

    First use mktime() on the input from the user

    $time = mktime(format from user);
    

    then do

    SELECT Project_Name, sdate, edate FROM table WHERE 
      UNIX_TIMESTAMP(STR_TO_DATE(sdate, '%e %m %Y')) <= '$time'
     AND 
      UNIX_TIMESTAMP(STR_TO_DATE(edate, '%e %m %Y')) >= '$time'
    

    That should work.

    0 讨论(0)
提交回复
热议问题