Jump to content

TimeStamp Format Validation


SchweppesAle

Recommended Posts

hi, I'm trying to use the following function in order to validate input as it's entered into the database.  All input should follow  the date('Y-m-d') format. If the function returns false I just enter a default value.  Seems there's something  wrong with the following algorithm though.

 

/*make sure date is valid and follows Y-m-d format*/
   function dateValidation($date)
   {
       /*first array element must be between 1970 and 2038*/
       /*second element must be between 01 and 12*/
       /*third between 01 and 31*/
       $validity = TRUE;

       $date = explode('-', $date);
       if($date[0] > 2038 || $date[0] < 1970)
       {
            $validity = FALSE;
       }
       
       if($date[1] > 12 || $date[1] < 01)
       {
            $validity = FALSE;
       }
       
       if($date[2] > 31 || $date[2] < 01)
       {
            $validity = FALSE;
       }

       /*must be equal to 3*/
       $size = count($date);
       if($size != 3)
       {
            $validity = FALSE;
       }

            return $validity;
   }

Link to comment
https://forums.phpfreaks.com/topic/186903-timestamp-format-validation/
Share on other sites

tried a new approach, still no luck though.  :shrug:

 

   /*make sure date is valid and follows Y-m-d format*/
   function dateValidation($date)
   {

       $validity = TRUE;

       $date = explode('-', $date);

       /*must be equal to 3*/
       $size = count($date);
       if($size != 3)
       {
            $validity = FALSE;
       }

       if(!checkdate($date[1], $date[2], $date[0]))
       {
            $validity = FALSE;
       }

            return $validity;
   }

  function dateValidation($date)
   {
        @list ($year, $month, $day) = explode('-', $date);
        return (strlen($year) === 4 && strlen($month) === 2 && strlen($day) === 2) ? checkdate($month,$year,$day) : false;
   }

  function dateValidation($date)
   {
        @list ($year, $month, $day) = explode('-', $date);
        return (strlen($year) === 4 && strlen($month) === 2 && strlen($day) === 2) ? checkdate($month,$year,$day) : false;
   }

 

still no good :/

 

It only seems to work when the field is left blank.

how are you calling the function consider the following

 

<?php
   function dateValidation($date)
   {
        @list ($year, $month, $day) = explode('-', $date);
        return (strlen($year) === 4 && strlen($month) === 2 && strlen($day) === 2) ? checkdate($month,$year,$day) : false;
   }
   var_dump(dateValidation(""));

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.