how to use php date in mysql query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tinus
    New Member
    • Jul 2010
    • 30

    how to use php date in mysql query

    Hi

    I need to run a query based on a php date variable.

    Code:
    $add2days=strtotime(date("Ymd",$MyToday). "+2 day");
    echo".....date(add2days)=".date('Y-m-d',$add2days)
    
    $query2 = "SELECT `daydate` FROM publicholiday WHERE `daydate`=`date('Y-m-d',$add2days)`";
    $aresult = $dbLink->query($query2);  
    	//$resultrow = $aresult->fetch_row();
    	//echo"sunday holiday row is".$resultrow[0];
        if($aresult)
         {.... }
    if($aresult) is false when it should actually be true for the date I am sending

    Thanks for the help!!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if($aresult) is false when it should actually be true for the date I am sending
    how do you know, that it *should* be true? actually, your query should either return noting or cause an error.

    PS. the MySQL DATE() function expects only one parameter.
    Last edited by Dormilich; Sep 27 '10, 09:37 AM.

    Comment

    • Tinus
      New Member
      • Jul 2010
      • 30

      #3
      Hi

      well, I have a mysql table in which all public holidays are stored.
      A date is entered into an html form. Then my php tests if this day is a holiday.
      In this specific section; it looks if the public holiday is on a sunday. If it is monday will be a holiday .

      What I am actually asking is if the following will return a result or will it return an error, or empty result.
      Code:
      $query2 = "SELECT `daydate` FROM publicholiday WHERE `daydate`=`date('Y-m-d',$add2days)`"; 
      $aresult = $dbLink->query($query2);
      The date is in the mysql table so if done correctly it should return a result.
      Last edited by Dormilich; Sep 27 '10, 09:43 AM. Reason: spelling

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        that’s the part (in bold), that's not done correctly. thus you *should* get an error.
        Code:
        $query2 = "SELECT `daydate` FROM publicholiday WHERE `daydate`=[B]`date('Y-m-d',$add2days)`[/B]";

        Comment

        • Tinus
          New Member
          • Jul 2010
          • 30

          #5
          Ok,thanks

          How should I do it then ?

          Code:
          echo $_POST["inputdate"];
          $indate=$_POST["inputdate"];
          echo " indate is".$indate;
          if (isset($_POST["inputdate"])){ 
          $MyToday=date("Ymd");
          $MyToday=strtotime($indate);}
          else{
          $MyToday=$today;}
          
          if((date("D",$MyToday))=='Fri') 
             {
             	$add2days=strtotime(date("Ymd",$MyToday). "+2 day");
          	echo".....date(add2days)=".date('Y-m-d',$add2days);
             $query2 = "SELECT daydate FROM publicholiday WHERE daydate=date('Y-m-d',$add2days)";
             //(SELECT DATE_FORMAT( (SELECT DATE_ADD(`$Mytoday`,INTERVAL 2 DAY ) ) ,'%Y-%m-%d'))";
              $aresult = $dbLink->query($query2);  
          	//$resultrow = $aresult->fetch_row();
          	//echo"sunday holiday row is".$resultrow[0];
              if($aresult)
               { //Vrydag en vakansiedag val op sondag
                 echo "public holiday on sunday so monday will be holiday";
                 //doen f,s,s,m
                 exec("C:/aptana/xampp-win32-1.7.3/xampp/htdocs/sas_prog >sas_prog.out");
                 exec("Psexec.exe -i -d  sas.exe");   
                  }

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            How should I do it then ?
            for a start, by not putting PHP functions inside a string. (this only works in some special cases)

            this would make way more sense:
            Code:
            $query2 = "SELECT `daydate` FROM `publicholiday` WHERE `daydate` = " . date('Y-m-d',$add2days);

            Comment

            • Tinus
              New Member
              • Jul 2010
              • 30

              #7
              :) Thank you for the help.It's working now.

              Comment

              Working...