Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    input form date check help

    Im trying to check a date is equal to after the current date but i cant same to get it to work, it just bringing up an error, can anyone help?

    My code is:

    PHP Code:
    if $date >= NOW or if ($date >= date();  
      {
        
        
    $error_stat 1

       
    //Set the message to tell the user the date is invalid for the year entered
        
    $date_message '*Invalid date*';
      } 

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    I'm not sure whether it's possible to compare date in PHP like that, but you what was the error?
    PHP Code:
    if $date >= NOW or if ($date >= date();  
      { 
    if should be of the form
    Code:
    if(condition1 || condition2)
    in your case.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    What is $date and what do you think NOW is?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by abduraooft View Post
    I'm not sure whether it's possible to compare date in PHP like that, but you what was the error?

    if should be of the form
    Code:
    if(condition1 || condition2)
    in your case.
    Thanks for the reply, I have the following code, but its not working properly, just outputs invalid date, can you help here?

    PHP Code:
    if ($date <= NOW || $date <= date);   
      { 
         
        
    $error_stat 1;  

       
    //Set the message to tell the user the date is invalid for the year entered 
        
    $date_message '*Invalid date*'
      } 

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by _Aerospace_Eng_ View Post
    What is $date and what do you think NOW is?
    $date is the variable name i have for the input field date, am i doing this completely wrong?

    This is the full code for the date check

    Code:
    //Date check) 
    
    if (empty($date)) {
    //Set the error_stat to 1, which means that an error has occurred
    	$error_stat = 1;
    
    //Set the message to tell the user to enter a dob
    $date_message = '*Please enter job closing date*';
    }
    
    //Check the format and explode into $parts
      elseif (!ereg("^([0-9]{4})/([0-9]{2})/([0-9]{2})$", 
              $date, $parts)){
     $error_stat = 1; 	
    
    //Set the message to tell the user the date is invalid
    $date_message = '*Invalid closing date, must be YYYY/MM/DD format*';
    }
        
      elseif (!checkdate($parts[2],$parts[1],$parts[3]))
      {
      $error_stat = 1; 
      
      //Set the message to tell the date is invalid for the month entered
    	$date_message = '*Invalid date, month must be between 1-12*';
    }
    
    if ($date <= NOW || $date <= date);   
      { 
         
        $error_stat = 1;  
    
       //Set the message to tell the user the date is invalid for the year entered 
        $date_message = '*Invalid date*'; 
      }

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I guess what I'm asking is what is the form of $date? What does the example input look like? Also again what do you think NOW is? It almost seems like you are trying to get NOW() from mysql yet you don't connect anywhere to mysql. And yes you are doing this wrong. We need more info though. Here is an example of how you can determine if the date is pass the current date or equal to it.
    PHP Code:
    <?php
    $date 
    mktime(000312008);
    echo 
    'Date is : ' $date .' milliseconds since January 1 1970 00:00:00 GMT.<br>';
    echo 
    'The time since January 1 1970 00:00:00 GMT is ' time() . ' in milliseconds.<br>';
    if (
    $date >= time())
    {
        
    $error_stat 1
        
    //Set the message to tell the user the date is invalid for the year entered
        
    $date_message '*Invalid date*';
    }
    echo 
    'Date message is ' $date_message '<br>';
    echo 
    'Error stat is ' $error_stat;
    ?>
    You use mktime() to create a unix timestamp.
    Last edited by _Aerospace_Eng_; 02-21-2008 at 04:02 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by _Aerospace_Eng_ View Post
    I guess what I'm asking is what is the form of $date? What does the example input look like? Also again what do you think NOW is? It almost seems like you are trying to get NOW() from mysql yet you don't connect anywhere to mysql.

    The input is yyyy/mm/dd in the database and the type is set as "date" in the database. Im not sure what i was doin NOW() Sorry for the confusion. Appreciate your help on this.

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try this
    PHP Code:
    <?php
    $date 
    '2008/02/21';
    $datetime strtotime($date);
    $todaysdate strtotime(date('Y\/m\/d'));
    if (
    $datetime >= $todaysdate
    {
        
    $error_stat 1
        
    //Set the message to tell the user the date is invalid for the year entered
        
    $date_message '*Invalid date*';
    }
    else
    {
        
    $error_stat 0;
        
    $date_message '*Valid date*';
    }
    echo 
    'Error stat is: ' $error_stat '<br>';
    echo 
    'Date message is: ' $date_message;
    ?>
    Edit: I was using time() instead of todays date. Changes should be correct.
    Last edited by _Aerospace_Eng_; 02-21-2008 at 04:16 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    PRodgers4284 (02-21-2008)

  • #9
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Try this
    PHP Code:
    <?php
    $date 
    '2008/02/21';
    $datetime strtotime($date);
    $todaysdate strtotime(date('Y\/m\/d'));
    if (
    $datetime >= $todaysdate
    {
        
    $error_stat 1
        
    //Set the message to tell the user the date is invalid for the year entered
        
    $date_message '*Invalid date*';
    }
    else
    {
        
    $error_stat 0;
        
    $date_message '*Valid date*';
    }
    echo 
    'Error stat is: ' $error_stat '<br>';
    echo 
    'Date message is: ' $date_message;
    ?>
    Edit: I was using time() instead of todays date. Changes should be correct.


    Thanks for that aerospace, that worked fine, appreciate your help


  •  

    Posting Permissions

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