简体   繁体   English

以正确的格式比较日期

[英]Comparing dates in the correct formats

I am having all sorts of issues trying to find matching formats to compare against and I can't understand how it can be so hard to compare 2 x dates and I guess it's just above me. 我遇到各种各样的问题,试图找到匹配的格式进行比较,但我不明白如何比较2个日期会变得如此困难,我想它就在我眼前。

I have a date in a MySQL database: 20/12/17 我在MySQL数据库中有一个日期:20/12/17

The report date is: 19/01/18 (it will eventually be todays date but I am testing on an old file) 报告日期为:19/01/18(最终将是今天的日期,但我正在测试旧文件)

I just want to find dates in 0-30 days, 31-60 days, 61-90 days and 91+ days 我只想找到0-30天,31-60天,61-90天和91+天的日期

I convert the dates to timestamp so I can do a comparison > or < but I'm having trouble moving the date to 30 days. 我将日期转换为时间戳,因此可以进行>或<的比较,但是我无法将日期移至30天。

I tried converting to a DateTime and using date_sub but it seems to convoluted... 我尝试转换为DateTime并使用date_sub,但似乎令人费解...

All I want to do is check to see if the date string in the db is within one of the date ranges but i'm struggling. 我要做的就是检查数据库中的日期字符串是否在日期范围之一内,但是我很挣扎。

I try using strtotime with '-30 days' I tried DateTime with date_sub 我尝试将strtotime与“ -30天”一起使用,将DateTime与date_sub一起使用

I think I am just really confused now and can't figure anything out 我想我现在真的很困惑,无法解决任何问题

Here is what I have: 这是我所拥有的:

        $RO_Date = date_format( date_create_from_format( 'd/m/y',  $RO["RO Date"] ), 'Y-m-d' );
        $todaysDate = date_format(date_create_from_format('d/m/y', '19/01/18'), 'Y-m-d' );// Using date file was sent

        echo $RO_Date;
        echo ' ('.strtotime( $RO_Date).') ';
        echo ' <br> ';
        echo $todaysDate;
        echo ' (' . strtotime($todaysDate) . ') ';
        echo ' <br> ';
        echo ' -30 Days ';
        echo ' (' . date("Y-m-d", strtotime("-30 days", $todaysDate)) . ') ';

        if (strtotime($RO_Date) >= $todaysDate-30 ) :
            echo ':CURRENT<br>';
        elseif (strtotime($RO_Date) >= $todaysDate-60 ) :
            echo ':31-60<br>';
        elseif (strtotime($RO_Date) >= $todaysDate - 90 ) :
            echo ':61-90<br>';
        elseif (strtotime($RO_Date) >= $todaysDate - 120 ) : 
            echo ':90-120<br>';
        else :
            echo ':120+<br>';
        endif;

Output: 输出:

2017-12-20 (1513688400) 
2018-01-19 (1516280400) 
-30 Days (1969-12-02) :CURRENT
2018-01-03 (1514898000) 
2018-01-19 (1516280400) 
-30 Days (1969-12-02) :CURRENT
2018-01-05 (1515070800) 
2018-01-19 (1516280400) 
-30 Days (1969-12-02) :CURRENT
2018-01-12 (1515675600) 
2018-01-19 (1516280400) 
-30 Days (1969-12-02) :CURRENT
2018-01-18 (1516194000) 
2018-01-19 (1516280400) 
-30 Days (1969-12-02) :CURRENT
2018-01-18 (1516194000) 
2018-01-19 (1516280400) 
-30 Days (1969-12-02) :CURRENT
<?php
    $todaysDate = date_format(date_create_from_format('d/m/y', '19/01/18'), 'Y-m-d' );// Using date 
    $todaysDate = strtotime($todaysDate);
    echo $todaysDate . PHP_EOL;
    $todaysDate = date("Y-m-d", strtotime("-30 days", $todaysDate));
    echo $todaysDate;

Output: 输出:

1516316400 1516316400

2017-12-20 2017-12-20

In this line 在这条线

$todaysDate = date("Y-m-d", strtotime("-30 days", $todaysDate));

$todaysDate needs to be a timestamp not a date string, that was the mistake $ todaysDate需要是一个时间戳而不是日期字符串,那是错误的

See http://php.net/strtotime http://php.net/strtotime

<?php
    function dateDiff($dateStringFromDB){
    $today     = new DateTime("Now");
    $db_string = new DateTime($dateStringFromDB);
    $days      = $today->diff($db_string)->format('%d'); 

    return $days;
    }
?>

The function above calculates the difference between the current date and any date passed to the function and returns the number of days. 上面的函数计算当前日期与传递给该函数的任何日期之间的差,并返回天数。 Example: if $dateStringFromDB is 17/04/2018 and $today is 15/04/2018, it returns the value of $days as 2. 示例:如果$ dateStringFromDB为17/04/2018并且$ today为15/04/2018,则它将$ days的值返回为2。

use: 采用:

echo dateDiff($dateStringFromDB);

You can now use the result for whatever you want. 现在,您可以将结果用于任何所需的内容。

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

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