简体   繁体   English

在php中比较日期的功能不起作用

[英]Function to compare dates in php doesn't work

I built this function that is supposed to compare dates and return a boolean. 我建立了应该比较日期并返回布尔值的函数。 When I run it though, it breaks, the page stops. 但是,当我运行它时,它中断了,页面停止了。

the function: 功能:

 public function compare_date_outlook($creation, $modification) {
        $creation_date =  DateTime::createFromFormat('Y-m-d\TH:i:s+', $creation);
        $creation_date = $creation_date->format( 'Y-m-d');
        $modification_date =  DateTime::createFromFormat('Y-m-d\TH:i:s+', $modification);
        $modification_date = $modification_date->format( 'Y-m-d');   
        $date = new \DateTime( 'yesterday' );
        $date->setTime( 0, 0, 0 );
        $yesterday = $date->format( 'Y-m-d');

           if (($creation || $modification) == $yesterday)
           {
               return TRUE;
           }

           else {
               return FALSE;
           }
        }

How I call it: 我怎么称呼它:

if ( compare_date_outlook($a['creationDate'], $a['lastModifiedDate']) === TRUE)

The format of the date: 日期格式:

 $a['creationDate'] = "2017-09-08T13:26:11.4354775Z";

It stops right in the beginnig: 它在开始时就停止了:

$creation_date =  DateTime::createFromFormat('Y-m-d\TH:i:s+', $creation);

Something like this should work: 这样的事情应该起作用:

function compare_date_outlook($creation, $modification)
{
    $today = (new \Datetime())->setTime(0, 0, 0);
    $yesterday = (new \Datetime('yesterday'))->setTime(0, 0, 0);

    $creation_date = \DateTime::createFromFormat('Y-m-d\TH:i:s+', $creation);
    $modification_date = \DateTime::createFromFormat('Y-m-d\TH:i:s+', $modification);

    return ($creation_date >= $yesterday && $creation_date < $today) ||
        ($modification_date >= $yesterday && $modification_date < $today);
}

Probably you have other namespace. 可能您还有其他名称空间。 So you need to add \\ before global methods/classes when you are in different namespace 因此,当您位于其他命名空间中时,需要在全局方法/类之前添加\\

see doc here http://www.php.net/manual/en/language.namespaces.global.php 在这里查看文档http://www.php.net/manual/en/language.namespaces.global.php

public function compare_date_outlook($creation, $modification) {
        $creation_date =  \DateTime::createFromFormat('Y-m-d\TH:i:s+', $creation);
        $creation_date = $creation_date->format( 'Y-m-d');
        $modification_date =  \DateTime::createFromFormat('Y-m-d\TH:i:s+', $modification);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM