在我的經驗,很多的問題都歸結到服務器上的區域設置。
PHP可能被識別DD /月/年爲毫米/日/年和具有配合如果一天的部分是無效的。爲了使PHP正確驗證日期DD/MM/YYYY,交換/爲 - (DD-MM-YYYY)時
$launch_date = $_POST['launch_date'];
$launch_date = str_replace("/", "-", $launch_date);
$launch_date = strtotime($launch_date);
這僅僅是一個快速的黑客攻擊,看看它的情況。如果不是的話,那麼我建議至少在一路上加入var_dumps來檢查日期格式是否正確。
另一種方法是解析日期以及使用date_create_from_format(http://php.net/manual/en/datetime.createfromformat.php)解析的格式。
$launch_date = date_create_from_format('d/m/Y', $launch_date);
我不認爲有在Windows上PHP的實現......因此,如果你遇到麻煩,這裏有一個功能的實現
if (!function_exists('date_parse_from_format')) {
function date_parse_from_format($format, $date) {
$returnArray = array('hour' => 0, 'minute' => 0, 'second' => 0,
'month' => 0, 'day' => 0, 'year' => 0);
$dateArray = array();
// array of valid date codes with keys for the return array as the values
$validDateTimeCode = array('Y' => 'year', 'y' => 'year',
'm' => 'month', 'n' => 'month',
'd' => 'day', 'j' => 'day',
'H' => 'hour', 'G' => 'hour',
'i' => 'minute', 's' => 'second');
/* create an array of valid keys for the return array
* in the order that they appear in $format
*/
for ($i = 0; $i <= strlen($format) - 1; $i++) {
$char = substr($format, $i, 1);
if (array_key_exists($char, $validDateTimeCode)) {
$dateArray[$validDateTimeCode[$char]] = '';
}
}
// create array of reg ex things for each date part
$regExArray = array('.' => '\.', // escape the period
// parse d first so we dont mangle the reg ex
// day
'd' => '(\d{2})',
// year
'Y' => '(\d{4})',
'y' => '(\d{2})',
// month
'm' => '(\d{2})',
'n' => '(\d{1,2})',
// day
'j' => '(\d{1,2})',
// hour
'H' => '(\d{2})',
'G' => '(\d{1,2})',
// minutes
'i' => '(\d{2})',
// seconds
's' => '(\d{2})');
// create a full reg ex string to parse the date with
$regEx = str_replace(array_keys($regExArray), array_values($regExArray), $format);
// Parse the date
preg_match("#$regEx#", $date, $matches);
// some checks...
if (!is_array($matches) ||
$matches[0] != $date ||
sizeof($dateArray) != (sizeof($matches) - 1)) {
return $returnArray;
}
// an iterator for the $matches array
$i = 1;
foreach ($dateArray AS $key => $value) {
$dateArray[$key] = $matches[$i++];
if (array_key_exists($key, $returnArray)) {
$returnArray[$key] = $dateArray[$key];
}
}
return $returnArray;
}
}
編輯:萬一這是錯過了.. 。確保你然後將其轉換成它什麼時候到DB YYYY-MM-DD:
// Whatever you do to parse date into timestamp
$launch_date_db = date("Y-m-d H:i:s", $launch_date)
UPDATE
PHP喜歡時間戳...所以與去:
$launch_date = strtotime($row['start_date']); // convert it into timestamp
echo date('d-m-Y', $launch_date); // format timestamp
如果它存儲在數據庫中的日期,它的存儲是正確的。格式只在顯示數據時很重要。 – 2014-09-24 15:20:21
但它沒有將其存儲在數據庫中,它顯示爲0000-00-00 – Lee 2014-09-24 15:21:01
什麼是數據庫列類型? – RandomSeed 2014-09-24 15:22:17