第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

碳日期比較和日期格式

碳日期比較和日期格式

PHP
烙印99 2024-01-19 17:06:29
我正在嘗試以下操作:我正在開(kāi)發(fā)一個(gè)函數(shù)來(lái)比較以下格式的兩個(gè)日期:$date = "Mon Sep 14 2020 02:07:25 GMT+0000 (Coordinated Universal Time)";該函數(shù)如下(它有一些用于我的測(cè)試的額外代碼):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”是將字符串日期轉(zhuǎn)換為 Carbon 類型的函數(shù),如下所示: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);}
查看完整描述

2 回答

?
湖上湖

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊

最近的日期大于較早的日期。所以你必須改變比較的方向。

為什么不直接比較 Carbon 對(duì)象而不是將它們轉(zhuǎn)換為字符串?嘗試這個(gè)

if ($newVariationFormat->greaterThan($oldVariationFormat))

前面的代碼通常應(yīng)該返回 true;


查看完整回答
反對(duì) 回復(fù) 2024-01-19
?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

Carbon 對(duì)象還可以與標(biāo)準(zhǔn)運(yùn)算符進(jìn)行比較:

if ($newVariationFormat > $oldVariationFormat)

并且您重新格式化Date可以縮短:

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


查看完整回答
反對(duì) 回復(fù) 2024-01-19
  • 2 回答
  • 0 關(guān)注
  • 149 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)