【问题标题】:Issue with dates [ first of month ] php and Carbon日期 [第一个月] php 和 Carbon 的问题
【发布时间】:2017-06-01 09:58:50
【问题描述】:

我在比较日期时遇到问题。当我想知道日期(默认 php 日期)是否在两个碳日期之间时。我在每月的第一天得到了不同的结果。

$date = date("2017-05-01");
    $date2 = date("2017-05-31");
    $since = Carbon::now()->firstOfMonth();
    $to = Carbon::now()->lastOfMonth();//->subDay();//$now->lastOfMonth();
    $this->info('since '.$since);
    $this->info('to '.$to);
    $this->info('date '.$date);
    $this->info('date2 '.$date2);
    $this->info("-------------------");
    if($date>= $since && $date <= $to ){
        $this->info('date in');
    }else{
        $this->info('date out');
    }
    if($date2>= $since && $date2 <= $to ){
        $this->info('date2 in');
    }else{
        $this->info('date2 out');
    }

输出是:

since 2017-05-01 00:00:00
to 2017-05-31 00:00:00
date 2017-05-01
date2 2017-05-31
-------------------
date out
date2 in

我希望 $date 输出是“日期”。 怎么了?

【问题讨论】:

    标签: php date laravel-5 php-carbon


    【解决方案1】:

    您正在比较 $date、$since 和 $to 之间的不同变量类型,因此输出很奇怪。 如果你运行这个:

           dd($date, $since, $to); 
    

    你会看到 $date 是一个字符串,另外两个是对象。

    你应该比较他们的时间戳:

            $date = strtotime("2017-05-01");
            $date2 = strtotime("2017-05-31");
            $since = Carbon::now()->firstOfMonth()->timestamp;
            $to = Carbon::now()->lastOfMonth()->timestamp;
    

    格林威治标准时间现在是 6 月,所以这也会导致您的程序输出“日期过期”

    【讨论】:

    • 谢谢!我就是这样做的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 2016-09-09
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多