我正在进行一项练习,其中我接受用户定义timestamp的日,月,年,小时,分钟,秒,然后显示所述时间以及多个时区的变化,同时还考虑每个时区场景包括无效输入。
我的问题是,在将其转换为时间之前,如何比较字符串的格式与时间?我首先尝试这样做,但得知date()不接受字符串。
$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;
}
答案 0 :(得分:1)
date()
的第二个参数必须是时间戳。使用strtotime()
将您的日期转换为时间戳:
if (date($format, strtotime($queryTime)) == $queryTime) {
} elseif (date($format,strtotime($queryTime)) !== $queryTime) {
最后一行真的只能是一个else
声明。
if (date($format, strtotime($queryTime)) == $queryTime) {
} else {
}