Reputation: 36003
I'm working on an exercise in which I accept a user defined timestamp with day, month, year, hour, min, sec and then display said time with variations across several timezones while also considering every scenario including invalid input.
My question is, how do I compare the format of a string to time before converting it to time? I first attempted to do it like so but learned that date() will not accept a string.
$format = 'd-m-Y h:i:s';
$queryTime = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year'] . " " . $_POST['hour'] . ":" . $_POST['min'] . ":" . $_POST['sec'];
$checkInput = date($format, $queryTime) == $queryTime;
if (date($format,$queryTime) == $queryTime) {
$now = strtotime($queryTime);
} elseif (date($format,$queryTime) !== $queryTime) {
$prompt = "Your input is invalid datetime. Try again.";
echo '<script>alert('.$prompt.');</script>';
exit;
}
Upvotes: 0
Views: 41
Reputation: 219864
The second parameter of date()
must be a timestamp. Use strtotime()
to convert your date into a timestamp:
if (date($format, strtotime($queryTime)) == $queryTime) {
} elseif (date($format,strtotime($queryTime)) !== $queryTime) {
The last line really could just an else
statement.
if (date($format, strtotime($queryTime)) == $queryTime) {
} else {
}
Upvotes: 1