php检查有效日期,奇怪的日期转换 [英] php check for a valid date, weird date conversions
问题描述
有没有办法检查一下日期/时间是否有效,你会认为这样会很容易检查:
Is there a way to check to see if a date/time is valid you would think these would be easy to check:
$date = '0000-00-00';
$time = '00:00:00';
$dateTime = $date . ' ' . $time;
if(strtotime($dateTime)) {
// why is this valid?
}
真正得到我的是:
echo date('Y-m-d', strtotime($date));
结果:1999-11-30,
results in: "1999-11-30",
嗯?我从0000-00-00到1999-11-30?
huh? i went from 0000-00-00 to 1999-11-30 ???
我知道我可以做比较,看看日期是否是这些值是相等的到目前为止,但它不是一个非常强大的检查方式。有没有一个很好的方法来检查我是否有有效的日期?任何人都有很好的功能来检查这个?
I know i could do comparison to see if the date is either of those values is equal to the date i have but it isn't a very robust way to check. Is there a good way to check to see if i have a valid date? Anyone have a good function to check this?
编辑:
人们问我正在运行什么:
运行PHP 5.2.5(cli在Linux本地主机2.6.18-53.1.14.el5#1 SMP Wed 3 5 11:36:49 EST 2008 i686 i686 i386 GNU / Linux
People are asking what i'm running: Running PHP 5.2.5 (cli) (built: Jul 23 2008 11:32:27) on Linux localhost 2.6.18-53.1.14.el5 #1 SMP Wed Mar 5 11:36:49 EST 2008 i686 i686 i386 GNU/Linux
推荐答案
从 php.net
<?php
function isValidDateTime($dateTime)
{
if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return true;
}
}
return false;
}
?>
这篇关于php检查有效日期,奇怪的日期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!