碳日期比较和日期格式

问题描述

我正在尝试以下操作:

我正在开发一种函数,用于比较以下格式的两个日期:

$date = "Mon Sep 14 2020 02:07:25 GMT+0000 (Coordinated Universal Time)";

函数如下(它为我的测试提供了一些额外的代码):

private function isMoreRecent($newVariation,$oldVariation) {
    // dates for testing:
    $newVariation = "Mon Sep 14 2020 02:07:25 GMT+0000 (Coordinated Universal Time)";
    $oldVariation = "Sun Sep 13 2020 12:02:49 GMT+0000 (Coordinated Universal Time)";
    // dates for testing:

    // date: 2020-09-14 02:07:25.0 UTC (+00:00)
    $newVariationFormat = $this->reformatDate($newVariation);
   

    // date: 2020-09-13 12:02:49.0 UTC (+00:00)
    $oldVariationFormat = $this->reformatDate($oldVariation);
    
    if ($newVariationFormat->toDateString() < $oldVariationFormat->toDateString()) {
        dd('holaaa');
        return true;
    }
    return false;
}

“ reformatDate”是将字符串日期转换为Carbon类型的函数,如下所示:

private function reformatDate($date) {
    $month = substr($date,4,3);
    $month = intval($this->getMonthNumber($month));
    $day = intval(substr($date,8,2));
    $year = intval(substr($date,11,4));
    $hour = substr($date,16,2);
    $minutes = substr($date,19,2);
    $seconds = substr($date,22,2);

    return Carbon::create($year,$month,$day,$hour,$minutes,$seconds);
}

其中的getMonthNumber():

private function getMonthNumber($month) {
    $monthkeyvalues = [
        '1' => 'Jan','2' => 'Feb','3' => 'Mar','4' => 'Apr','5' => 'May','6' => 'Jun','7' => 'Jul','8' => 'Ago','9' => 'Sep','10' => 'Oct','11' => 'Nov','12' => 'Dec',];

    return array_search($month,$monthkeyvalues);
}

它返回这样的对象:

Carbon\Carbon @1600049245 {#255 ▼
#constructedobjectId: "0000000021e9258b000000005e04548d"
#localMonthsOverflow: null
#localYearsOverflow: null
#localStrictModeEnabled: null
#localHumanDiffOptions: null
#localToStringFormat: null
#localSerializer: null
#localMacros: null
#localGenericMacros: null
#localFormatFunction: null
#localTranslator: null
#dumpProperties: array:3 [▶]
#dumpLocale: null
date: 2020-09-14 02:07:25.0 UTC (+00:00)
}

好,我有以下问题:在我用于测试的两个日期中,

$newVariation = "Mon Sep 14 2020 02:07:25 GMT+0000 (Coordinated Universal Time)";
$oldVariation = "Sun Sep 13 2020 12:02:49 GMT+0000 (Coordinated Universal Time)";

$ newVariation早于$ oldVariation,所以为什么:

if ($newVariationFormat->toDateString() < $oldVariationFormat->toDateString()) {
        dd('holaaa');
        return true;
    }
    return false;

让我返回false而不是true?

我一直在关注Carbon文档,据此,$ newVariationFormat必须小于$ oldVariationFormat。

我还使用了lessthan,$ first-> lessthan($ second)进行了测试,但是相同。

我在做什么错了?

非常感谢!

[EXTRAHELP]:我最终需要将日期转换为以下格式:

'2020-08-13T16:09:22.421Z'

是哪种格式?

谢谢。

解决方法

最近的日期大于更早的日期。因此,您必须更改比较的方向。

为什么不直接比较Carbon对象而不是将它们转换为字符串? 试试这个

<div class="o_kanban_tags_section oe_kanban_partner_categories">
    <span class="oe_kanban_list_many2many">
        <div class="o_field_many2manytags o_field_widget o_kanban_tags">
            <t t-foreach="get_m2m_data('category_id')" t-as="category_data">
                <span t-att-class="'o_tag o_tag_color_'+ category_data.data['color']"><span></span><t t-esc="category_data.data['display_name']"/>
                </span>
            </t>
        </div>
    </span>
</div>  

先前的代码通常应返回true;

,

碳对象也可以与标准运算符进行比较:

if ($newVariationFormat > $oldVariationFormat)

您可以重新格式化日期:

private function reformatDate($date) {
    return Carbon::parse(preg_replace('/\s+\(.*\)$/','',$date));
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...