簡體   English   中英

檢查日期是否為“ Ymd”格式,而不是將來的日期?

[英]Check if date is in 'Y-m-d' format and is not in future date?

我有一個表格來捕獲將以日期格式顯示的帖子輸入字段,我需要檢查此帖子字段是否為Ymd格式並且日期不位於任何將來的日期。

$dob = $_POST['dob']; 
// 1995-11-03 => Correct format
// 1995-30-12 => Incorrect format
// 2018-09-23 => Incorrect future date
list($year, $month, $day) = explode('-', $_POST['dob']);
$timestamp = mktime(0, 0, 0, $month, $day, $year);

// Check if the date is valid, and that it's in the past
$isValid = checkdate($month, $day, $year) && $timestamp <= time();

手冊: http//php.net/manual/en/function.checkdate.phphttp://php.net/manual/en/function.mktime.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM