laravel 使用 Carbon 了解时间是否属于两个时间点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27245776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using Carbon to know if a time falls under two points of time or not
提问by Nirmalz Thapaz
I am using Laravel. I have just come across Carbon
which was already there in vendors folder. I did some quick search and found that it is a great extension from PHP Date time.
我正在使用 Laravel。我刚刚发现Carbon
它已经存在于供应商文件夹中。我做了一些快速搜索,发现它是 PHP 日期时间的一个很好的扩展。
Somewhere in my application, I needed a logic that will check if a given time falls between two points of time. Lets say, I want to check if 10:00 falls under the time time 9:55 and 10:05. Surely, it falls but how should I use that logic with the help of Carbon.
在我的应用程序的某个地方,我需要一个逻辑来检查给定的时间是否落在两个时间点之间。假设,我想检查 10:00 是否属于时间时间 9:55 和 10:05。当然,它会下降,但是我应该如何在 Carbon 的帮助下使用该逻辑。
By default Carbon::now()
will return date and time in 2014-12-02 14:37:18
format. I was thinking if I could extract the time part only i.e 14:37:18
then I can compare two times to know whether the time under testing falls under the two points of time or not.
默认情况下Carbon::now()
将以2014-12-02 14:37:18
格式返回日期和时间。我在想如果我只能提取时间部分,14:37:18
那么我可以比较两次以知道被测时间是否落在两个时间点之下。
If i directly check two Carbon objects, it will try to check the year as well. But all I need is just the time part only.
如果我直接检查两个 Carbon 对象,它也会尝试检查年份。但我需要的只是时间部分。
And as a matter of fact, I am not even sure If the times (h:m:s) can directly can be compared or not through carbon.
事实上,我什至不确定时间(h:m:s)是否可以通过碳直接比较。
回答by Kyslik
Yes there is a way to do this in Carbon, as just take a look on documentation here.
是的,在 Carbon 中有一种方法可以做到这一点,只需查看此处的文档即可。
To determine if the current instance is between two other instances you can use the aptly named between() method. The third parameter indicates if an equal to comparison should be done. The default is true which determines if its between or equal to the boundaries.
要确定当前实例是否在其他两个实例之间,您可以使用恰当命名的 between() 方法。第三个参数指示是否应该进行等于比较。默认值为真,确定其是否介于或等于边界。
$first = Carbon::create(2012, 9, 5, 1);
$second = Carbon::create(2012, 9, 5, 5);
var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false)); // bool(false)
回答by DusteD
UNIX time is easier for this sort of task.
对于此类任务,UNIX 时间更容易。
So you could use strtotime to convert your hipster representation to neckbeard representation and compare like a real man. Since you only want to compare hours, you can hack this by using a relative time.
因此,您可以使用 strtotime 将您的时髦表示转换为颈须表示并像真人一样进行比较。由于您只想比较小时数,因此您可以使用相对时间来解决这个问题。
<?php
$now = time();
$startTime = strtotime( "12:10:24", $now );
$endTime = strtotime( "14:24:45", $now );
$point = strtotime("12:25:40", $now );
if( $point >= $startTime && $point <= $endTime )
{
echo "Inside\n";
} else {
echo "Outside\n";
}
?>
Output:
输出:
$ php test.php 2> /dev/null
Inside