php 检查日期是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10461928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if date is empty
提问by user1347219
In a whileloop how can I have a date displayed onlyif that date exists?
在while循环中,如何仅在该日期存在时才显示日期?
E.g.: If date is 0000-00-00, display nothing.
例如:如果日期是0000-00-00,则不显示任何内容。
I am currently reading the date as follows, but I am getting 01-01-1970when 0000-00-00:
我目前正在读的日期如下,但是我得到01-01-1970的时候0000-00-00:
date('d-m-Y', strtotime($row['date']))
采纳答案by Dhruvisha
You should check first what is there in date field whether it contains date or its NULL. According to result you can read date .
您应该首先检查日期字段中是否包含日期或其NULL。根据结果,您可以读取 date 。
while ($row = mysql_fetch_array($result)) {
if (strtotime($row['date']) != '0000-00-00') {
mydate = date('d-m-Y', strtotime($row['date']));
} else {
mydate = '';
}
}
回答by Tushar Gohel
try this, its work for me
试试这个,它对我有用
while($row = mysql_fetch_array($result)){
if(strtotime($row['date']) > 0){
echo date('d-m-Y', strtotime($row['date']));
}else{
echo 'Date not valid or null';
}
}
回答by ffork
The currently accepted answer is wrong.
当前接受的答案是错误的。
The date "0000-00-00" is parsed as "-0001-11-30". This is explained in the Date Formatssection of the PHP manual:
日期“0000-00-00”被解析为“-0001-11-30”。PHP 手册的日期格式部分对此进行了解释:
It is possible to over- and underflow the dd and DD format. Day 0 means the last day of previous month, whereas overflows count into the next month. This makes "2008-08-00" equivalent to "2008-07-31" and "2008-06-31" equivalent to "2008-07-01" (June only has 30 days).
Note that as of PHP 5.1.0 the day range is restricted to 0-31 as indicated by the regular expression above. Thus "2008-06-32" is not a valid date string, for instance.
It is also possible to underflow the mm and MM formats with the value 0. A month value of 0 means December of the previous year. As example "2008-00-22" is equivalent to "2007-12-22".
If you combine the previous two facts and underflow both the day and the month, the following happens: "2008-00-00" first gets converted to "2007-12-00" which then gets converted to "2007-11-30". This also happens with the string "0000-00-00", which gets transformed into "-0001-11-30" (the year -1 in the ISO 8601 calendar, which is 2 BC in the proleptic Gregorian calendar).
可以上溢和下溢 dd 和 DD 格式。第 0 天表示上个月的最后一天,而溢出计数到下个月。这使得“2008-08-00”相当于“2008-07-31”,“2008-06-31”相当于“2008-07-01”(六月只有30天)。
请注意,从 PHP 5.1.0 开始,天范围被限制为 0-31,如上面的正则表达式所示。因此,例如,“2008-06-32”不是有效的日期字符串。
也可以使用值 0 下溢 mm 和 MM 格式。月份值为 0 表示前一年的十二月。例如,“2008-00-22”相当于“2007-12-22”。
如果您结合前两个事实并在日和月下溢,则会发生以下情况:“2008-00-00”首先转换为“2007-12-00”,然后转换为“2007-11-30” . 字符串“0000-00-00”也会发生这种情况,它会被转换为“-0001-11-30”(ISO 8601 日历中的第 -1 年,在公历中是公元前 2 年)。
If you're using an older version of PHP or a 32-bit operating system, strtotime("0000-00-00") will return false, but not because its recognizing it as not being set, but because the parsed date (November 30th, -0001) falls outside of what dates it can handle. Calling that same function on a 64-bit system with a newer verison of PHP will return an integer resembling -62169984000 (slight variance depending on the timezone of your system). This is because that system can handle the supplied date and properly returns the number of seconds between January 1st 1970 and November 30th, -0001.
如果您使用的是旧版本的 PHP 或 32 位操作系统, strtotime("0000-00-00") 将返回 false,但不是因为它认为它没有被设置,而是因为解析日期(November 30th, -0001) 超出了它可以处理的日期。在具有较新版本 PHP 的 64 位系统上调用相同的函数将返回一个类似于 -62169984000 的整数(根据系统的时区略有差异)。这是因为该系统可以处理提供的日期并正确返回 1970 年 1 月 1 日和 11 月 30 日 -0001 之间的秒数。
So using strtotime() on the string "0000-00-00" to determine if it has been set or not is improper use of the function and does not return the desired result. As the string "0000-00-00" is being considered the default unset value in this question, a simple string comparison will suffice:
因此,在字符串“0000-00-00”上使用 strtotime() 来确定它是否已设置是对函数的不当使用,并且不会返回所需的结果。由于字符串“0000-00-00”被视为此问题中的默认未设置值,因此简单的字符串比较就足够了:
while($row = mysql_fetch_array($result)){
if($row['date'] != '0000-00-00'){ // string comparison to see if set or not
echo date('d-m-Y', strtotime($row['date']));
}
}
Note that on 32-bit systems and older PHP versions this will only work for dates between Dec 13 1901 and Jan 19 2038. Dates outside of that will cause strtotime($row['date']) to return false, which the date() function will interpret as 0 and return "01-01-1970" as that is 0 seconds since Jan 1 1970.
请注意,在 32 位系统和较旧的 PHP 版本上,这仅适用于 1901 年 12 月 13 日和 2038 年 1 月 19 日之间的日期。超出该日期的日期将导致 strtotime($row['date']) 返回 false,即 date( ) 函数将解释为 0 并返回“01-01-1970”,因为这是自 1970 年 1 月 1 日以来的 0 秒。
回答by Rosu Flavius
An easy way to do this is to evaluate the $row['date'] as integer and test the value, like this:
一种简单的方法是将 $row['date'] 评估为整数并测试该值,如下所示:
if(!empty((int)$row['date']))
{
echo "NOT empty";
}
else echo "EMPTY";
OR short version:
或简短版本:
!empty((int)$row['date']) ? echo "NOT empty" : echo "EMPTY";
回答by Mischa Braam
What you could do is replace all 0, -, :and values from the date or datetime string and check if that's empty.
你可以做的是全部替换0,-,:并从日期或日期时间字符串和校验值,如果这是空的。
if (empty(str_replace(array("0", "-", ":", " "), "", $mydate)))
{
echo "empty date";
}
else
{
echo "not empty date";
}
回答by Er. Anurag Jain
Try this
尝试这个
foreach($row As $rows){
if(strtotime($rows['date']) == ''){
echo "N/A";
}else{
echo date('d-m-Y', strtotime($rows['date']));
}
echo "<br/>";
}
}
strtotime of "0000-00-00" AND "" is equal to "".
strtotime of "0000-00-00" AND "" is equal to "".

