#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,483
    Rep Power
    221

    Validate date and time. Taking science to the next level


    Hi;

    Does it have to be this long? Or can it be done with a magic wand?

    PHP Code:
    public function is_valid_date($date=null)
      {
        if(
    $date==null)
          {
            return 
    false;
          }
        
    $d explode("-"$date);  
        if(
    is_array($d) && count($d)==3)
          {
            if(!
    checkdate($d[1],$d[2],$d[0]))
              {
                return 
    false;
              }
            else 
              {
                return 
    true;
              }
          }    
        else 
          {
            return 
    false;
          }    
      }
    public function 
    is_valid_time($time=null)
      {
        if(
    $time==null)
          {
            return 
    false;
          }
        if(
    strtotime($time))
          {
            return 
    true;
          }          
        else 
          {
            return 
    false;
          }  
      } 
    PHP Code:
    public function is_plc_available_from_valid($plc_id)
    {
       
    $CI =& get_instance();
       if(
    $CI->launch_library->launch_type_by_plc_id($plc_id)=='evergreen')
        {
          return 
    true;
        }
       if(!isset(
    $_POST['broadcast_access_date']) || !isset($_POST['broadcast_access_time']) )
          {
            return 
    5;
          }
         if(!
    $CI->custom_functions->is_valid_date($_POST['broadcast_access_date']))
          {
            return 
    4;
          }
        if(!
    $CI->custom_functions->is_valid_time($_POST['broadcast_access_time']))
          {
            return 
    3;
          }  
        
        if(
    $CI->plc_library->launch_offer_open_date_time_by_plc_id($plc_id) < strtotime($_POST['broadcast_access_date']." ".$_POST['broadcast_access_time']))
          {
            return 
    2;
          }
        else 
          {
            return 
    1;
          }  

  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,752
    Rep Power
    9646
    If by "magic wand" you mean regular expressions then kinda. They can help.

    Validate a YYYY-MM-DD date?
    PHP Code:
    if (preg_match('/^(\d\d\d\d)-(\d\d)-(\d\d)$/'$input$match) && checkdate($match[1], $match[2], $match[0])) { 
    Validate a time?
    PHP Code:
    if (preg_match('/^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$/')) { 
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,483
    Rep Power
    221
    I love you but I don't like this solution.

    Why not something like:

    PHP Code:
    validate_date($date'YYYY-mm-dd H:i'); 
    The format is YYYY-mm-dd H:i

    Did you see my code?
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,752
    Rep Power
    9646

IMN logo majestic logo threadwatch logo seochat tools logo