是否有 PHP 函数来测试零日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5593955/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:55:45  来源:igfitidea点击:

Is there a PHP function to test a zero date

phpdatetime

提问by H. Ferrence

Looking for a shorthand or better way to test a zero date (MySQL datetime column) other than:

寻找一种速记或更好的方法来测试零日期(MySQL 日期时间列),而不是:

if ($row['this_date'] == '0000-00-00 00:00:00') echo 'No date set.';

Is there a PHP function that test it? Thanks.

是否有测试它的 PHP 函数?谢谢。

采纳答案by Your Common Sense

I think you don't need any shorthand.
It's sort of some micro-optimization.

我认为你不需要任何速记。
这是某种微观优化。

You have already spent more time asking this question than can save you such a shorthand in a year of average coding. Just write it and move on.
Don't you have more important problems to solve?

您已经花了更多的时间来问这个问题,而在一年的平均编码中却无法为您节省这样的速记。写下来然后继续。
你没有更重要的问题需要解决吗?

Moreover, with such a shorthand you will obfuscateyour own code.
Your current statement is perfectly clear, reads "if date is empty"
but all proposed snippets aren't that clear. Coming to this code months later, you will puzzle yourself with question, if such a code monster have any special meaning.

此外,使用这样的速记,您将混淆您自己的代码。
您当前的声明非常清楚,阅读"if date is empty"
但所有建议的片段都不是那么清楚。几个月后来到这个代码,你会疑惑,这样的代码怪物是否有什么特殊含义。

What you actually wanted is readability. But you have it already. While all proposed shorthands don't.

你真正想要的是可读性。但你已经拥有了。虽然所有建议的速记都没有。

回答by Jonathan Gimeno

What about:

关于什么:

if(strtotime($row['this_date']) == 0) echo 'No date set.';

回答by Khez

Although you should probably use NULL in your database, a simple hack would be:

尽管您可能应该在数据库中使用 NULL,但一个简单的技巧是:

if(!(int)$row['this_date'])echo'No Date Set.';

What kind of better way are you expecting to find? it's fast as it is.

您希望找到哪种更好的方法?它很快。

P.S. It's a hack because I'm assuming you will have a year set. Meaning it won't work for the string 0000-00-00 00:00:01

PS 这是一个黑客,因为我假设你会有一年。这意味着它不适用于字符串0000-00-00 00:00:01

回答by syck

Being a fan of regular expressions, I'll add another one:

作为正则表达式的粉丝,我将添加另一个:

if (!preg_match('/[1-9]/', $row['this_date'])) echo 'No date set.';
  • Works also with timestamps and other stuff.
  • Obviously does not check for date consistency.
  • 也适用于时间戳和其他东西。
  • 显然不检查日期一致性。

回答by Manu

if (strtotime($row['this_date'])) echo 'No date set.';

回答by berkayunal

You can use the following code

您可以使用以下代码

$date = '0000-00-00 00:00:00';
if(str_replace(array("-", " ", ":"), "", $date)*1>0){
    echo "DATE";
} else {
    echo "NODATE";
}

回答by Ravi Hirani

This solution works for me.

这个解决方案对我有用。

'0000-00-00'value is stored in my db table.

'0000-00-00'值存储在我的数据库表中。

function checkValidDate($date){
   $timestamp = strtotime($date);
   return (!empty($timestamp) && $date!='0000-00-00') ? date('d-M-Y',$timestamp) : null;
}

回答by Heroselohim

It should be avoided at all costs to use and store zero dates. This function could help close the gap until they are fully removed from your code. May be it could be called "isEmptyDate" or "isNullDate".

应该不惜一切代价避免使用和存储零日期。此功能可以帮助缩小差距,直到它们从您的代码中完全删除。也许它可以被称为“isEmptyDate”或“isNullDate”。

// Returns 'true' for zero, zero dates or times, null, false
function isZeroDate($date)
{
  if (empty($date)) return true;

  if (is_string($date)) {
    // Extract any character that it's not digit and convert it to int
    // 0000-00-00 00:00 || 0000 || 00:00 || 0000-00-00 will return true
    $ival = intval(preg_replace('~\D~', '', $date), 10);
    if ($ival == 0) return true;
  }

  return false;
}

Carbonand DateTimeclasses cannot parse zero dates with this format "0000-00-00 00:00:00".

CarbonDateTime类无法使用这种格式“0000-00-00 00:00:00”解析零日期。

With any PHP framework like Laravel, use nullable columns in your migrations to store NULL dates in DB

使用 Laravel 等任何 PHP 框架,在迁移中使用可为空的列在数据库中存储空日期

eg. in Laravel:

例如。在 Laravel 中:

$table->timestamp('some_date')->nullable()->default(null);

eg in MySql:

例如在 MySql 中:

create table date_test (some_date datetime null default null);
insert into date_test (some_date) values (null);

回答by Daniel

I'm using:

我正在使用:

if ($timedate->format('U') == -62169962400){
   // 0000-00-00
}

format('U')will return Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)The number may need to be adjusted for the timezone

format('U')将返回Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)数字可能需要针对时区进行调整

回答by Ruth Taylor

Use checkdate

使用检查日期

<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
?>

The above example will output:

上面的例子将输出:

bool(true)
bool(false)

http://php.net/manual/en/function.checkdate.php

http://php.net/manual/en/function.checkdate.php