检查PHP日期时间戳正确吗? [英] Check PHP datetime stamp is correct?
本文介绍了检查PHP日期时间戳正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以检查日期时间戳在PHP中是否正确?
Is there a way of checking whether a date time stamp is correct in PHP?
我目前在MySQL中使用yyyy-mm-dd hh:mm:ss
,并希望确保用户何时以与正确格式匹配的形式提供日期/时间戳.
I am currently using yyyy-mm-dd hh:mm:ss
in MySQL and would like to ensure when a user provides the date/time stamp in a form it matches the correct format.
推荐答案
[直到有人自带良好(无错误),可运行的checkdate()示例) 我正在使用此功能:
[until someone comes with good (bugfree), working checkdate() example ] I am using this function:
<?php
function validateMysqlDate( $date ){
if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $date, $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return true;
}
}
return false;
}
// check it:
$a = validateMysqlDate('2012-12-09 09:04:00');
$b = validateMysqlDate('20122-12-09 09:04:00');
$c = validateMysqlDate('2012-12_09 09:04:00');
$d = validateMysqlDate('');
var_dump( $a );
var_dump( $b );
var_dump( $c );
var_dump( $d );
?>
和btw:尽管$ dateb不是有效的mysql日期时间,但checkdate()对于$ b将返回true
and btw: checkdate() would return true for $b although it is not a valid mysql datetime
这篇关于检查PHP日期时间戳正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文