[英]Issue with dates [ first of month ] php and Carbon
I have an issue comparing dates. 我在比较日期时遇到问题。 When I want to know if a date (default php date) is between two Carbon dates.
当我想知道某个日期(默认php日期)是否在两个Carbon日期之间时。 I get a difference in the first day of month.
我在每月的第一天有所不同。
$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');
}
The output is: 输出为:
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
I expect $date output be 'date in'. 我希望$ date输出是'date in'。 Whats wrong?
怎么了?
You are comparing different variable types between $date, $since and $to so the output is strange. 您正在比较$ date,$ since和$ to之间的不同变量类型,因此输出很奇怪。 If you run this:
如果运行此命令:
dd($date, $since, $to);
You will see that $date is a string and the other two are objects. 您将看到$ date是一个字符串,另外两个是对象。
You should be comparing their timestamp: 您应该比较它们的时间戳:
$date = strtotime("2017-05-01");
$date2 = strtotime("2017-05-31");
$since = Carbon::now()->firstOfMonth()->timestamp;
$to = Carbon::now()->lastOfMonth()->timestamp;
Also GMT time is in June now so that will also cause your program to output "date out" 此外,格林尼治标准时间现在是六月,因此这也会导致您的程序输出“ date out”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.