php 检查 $date 是否等于 0000-00-00 00:00:00

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

Check if $date is equal to 0000-00-00 00:00:00

phpdate

提问by Muhammad Imran Tariq

I want a logic if $due is equal to no date then I want data starting with 2000. My code below is not working.

我想要一个逻辑,如果 $due 等于没有日期,那么我想要从 2000 开始的数据。我下面的代码不起作用。

if($due == '0000-00-00 00:00:00'){
    $due =  strtotime('2000-00-00 00:00:00');
}

回答by Umbrella

If you are storing strtotime() in $due, then you need to compare it like that, too

如果您将 strtotime() 存储在 $due 中,那么您也需要像这样比较它

if($due == strtotime('0000-00-00 00:00:00')){
    $due =  strtotime('2000-00-00 00:00:00');
}

or, more simply

或者,更简单地说

if($due == 0){
    $due =  strtotime('2000-00-00 00:00:00');
}

but if $due is coming from your db as a string, you would want to strtotime() it:

但是如果 $due 作为字符串来自你的数据库,你会想要 strtotime() 它:

$due = strtotime($due);
if($due == 0){
    $due =  strtotime('2000-00-00 00:00:00');
}

Be aware of timezones, though. That is, strtotime('0000-00-00 00:00:00 UTC') != strtotime('0000-00-00 00:00:00 EST'). This could also break your compare.

不过要注意时区。也就是说,strtotime('0000-00-00 00:00:00 UTC') != strtotime('0000-00-00 00:00:00 EST')。这也可能会破坏您的比较。

回答by Hossein Khalesi

I don't know why strtotime didnt work correctly for me. Probably it's because of time zone and other stuff.

我不知道为什么 strtotime 对我不起作用。可能是因为时区和其他东西。

As always fancy regular expressions works like a charm. I'm somehow addicted to it!

像往常一样花哨的正则表达式就像一种魅力。我不知何故沉迷于它!

if(preg_match("/0{4}/" , $dbDateField))
{
    // no its not acceptable
}
else
{
    //ok
}

回答by Ricardo

I know that this is an old question but this answer may help somebody

我知道这是一个老问题,但这个答案可能对某人有所帮助

strtotime returns a timestamp on success, FALSE otherwise.

strtotime 返回成功的时间戳,否则返回 FALSE。

$test = strtotime($due);  
$due  = (!$test || $test < 0) ? '2000-00-00 00:00:00' : $due;

unset($test);

regards.

问候。

回答by DaveRandom

You need to ensure that you are comparing the values in the same format. With what you currently have, on of three things will happen:

您需要确保以相同的格式比较值。使用您目前拥有的,将会发生以下三件事:

  • If $dueis an int, the string you are comparing it with 0000-00-00 00:00:00will be converted to an int, which will result in 0, and $duewill be compared with 0. This will only result in a match if $due === 0.
  • If $dueis a bool, the string will be converted to a bool, which will result in TRUE, and you will only get a match if $due === TRUE.
  • If $dueis any other type, it will be converted to a string and the two will be compared as strings. You will only get a match if $due === '0000-00-00 00:00:00'.
  • 如果$due是 an int,则与它进行比较的字符串0000-00-00 00:00:00将转换为 an int,这将导致0,并将与$due进行比较0。如果 ,这只会导致匹配$due === 0
  • 如果$due是 a bool,则字符串将转换为 a bool,这将导致TRUE,并且您只会得到匹配 if $due === TRUE
  • 如果$due是任何其他类型,则将其转换为字符串,并将两者作为字符串进行比较。如果 ,您只会得到匹配项$due === '0000-00-00 00:00:00'

If would say that you probably need to compare the values as integers. This means that you need to pass any strings through strtotime()- including $due, if it is a string. Since you do not show an example value of $dueI cannot provide an exact working code.

如果会说您可能需要将值作为整数进行比较。这意味着您需要通过任何字符串传递strtotime()- 包括$due,如果它是一个字符串。由于您没有显示$dueI的示例值,因此无法提供确切的工作代码。

回答by Yousof K.

strtotime(YourDate) != NULLworks fine too.

strtotime(YourDate) != NULL工作正常。

I was working on a DOB and DOM (Date of Marriage) section of a Database. Seems the NULL default saved a date as 0000-00-00. Which is kind of a stupid thing to be honest.

我正在研究数据库的 DOB 和 DOM(结婚日期)部分。似乎 NULL 默认将日期保存为 0000-00-00。老实说,这是一件愚蠢的事情。

Or perhaps it was my stupidity to make the field a DATE field instead of making it a VARCHAR and hold the STRTOTIME value there.

或者,将字段设为 DATE 字段而不是将其设为 VARCHAR 并在那里保存 STRTOTIME 值可能是我的愚蠢。

<?php 
    if(strtotime($dates->dom) != NULL) 
    {
        echo '<a href="' . base_url() . 'dates/dom/' . $dates->dom . '">' . date('j F, Y',strtotime($dates->dom)) . '</a>';
    } 
    else 
    { 
        print 'Not Listed Yet';
    } ?></strong>

回答by Nikunj Dhimar

i would like to suggest simplest form

我想建议最简单的形式

if(!$due)
        $due =  strtotime('2000-00-00 00:00:00');

回答by Hasib Ullah Khan

Here is solution to check if datetime field in database (mysql) is null or '0000-00-00 00:00:00'

这是检查数据库(mysql)中的日期时间字段是否为空或'0000-00-00 00:00:00'

selecting all with starttime is null or '0000-00-00 00:00:00' and endtime is not ( null or '0000-00-00 00:00:00')

选择所有开始时间为空或'0000-00-00 00:00:00'而结束时间不是(空或'0000-00-00 00:00:00')

At database end

在数据库端

select * from table where IFNULL(MONTH(starttime),0) >= 1 and IFNULL(MONTH(endtime),0) < 1;

at phpend

php

if((int)$starttime && !(int)$endtime){  ///  }