0

Here is my input tag:

<input type="text" name="EndDate" value="<?php echo $StartDate; ?>">

I am using PHP as well so I have made a function to do this task:

function validateDate($date, $format = 'Y-m-d H:i:s'){
     $d = DateTime::createFromFormat($format, $date);
     return $d && $d->format($format) == $date)
}

function was copied from this answer or php.net

My question is how do I incorporate the function into the input tag? Or if there is an easier way please let me know! Thanks!

Community
  • 1
  • 1

1 Answers1

0

This looks like a case where a ternary statement would be helpful:

<?php $StartDate = validateDate($StartDate, 'Y-m-d H:i:s') ? $StartDate : ''; ?>

This uses the true/false return value of validateDate($StartDate, 'Y-m-d H:i:s') then assigns $StartDate on true or assigns an empty string '' on false. In short, it keeps valid dates and blanks out invalid dates.

Siphon
  • 1,041
  • 8
  • 17