laravel 如何在laravel中以不同的方式比较日期和时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28979693/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:06:50  来源:igfitidea点击:

How to compare date and time differently in laravel

javascriptjquerylaravel

提问by Parag Bhingre

I'm getting date and time from my customer side using laravel. How can we compare given date and time with database and return result to customer?

我正在使用 laravel 从我的客户那里获取日期和时间。我们如何将给定的日期和时间与数据库进行比较并将结果返回给客户?

For Example, if I am getting Date and time from customer and I want to show him rows which do not clash with his date and time.

例如,如果我从客户那里获取日期和时间,并且我想向他显示与他的日期和时间不冲突的行。

采纳答案by lukasgeiter

Actually you can do all of this in a DB query:

实际上,您可以在数据库查询中完成所有这些操作:

$date = '2015-03-11 06:29:56';
$alreadyBooked = DB::table('bookings')
                   ->whereRaw('ABS(TIMESTAMPDIFF(MINUTE, datetime, ?)) <= 120', [$date])
                   ->exists();

if($alreadyBooked){
    // show error
}

Explanation

解释

?is a placeholder for the $date. Those are called bindings and are mostly used to get a clean query and prevent SQL injection.

?是 的占位符$date。这些称为绑定,主要用于获得干净的查询并防止 SQL 注入。

TIMESTAMPDIFF(MINUTE, ...will return the difference of the input date time and the one stored in the database field in minutes. If that value is smaller or equal to 120 (2 hours = 120 minutes) it matches the condition.

TIMESTAMPDIFF(MINUTE, ...将返回输入日期时间与存储在数据库字段中的时间的差异(以分钟为单位)。如果该值小于或等于 120(2 小时 = 120 分钟),则它与条件匹配。

ABSwill make the value "unsigned". -60becomes 60

ABS将使值“无符号”。-60变成60

回答by mininoz

In Laravel you can use Carbon for comparing date/time

在 Laravel 中,您可以使用 Carbon 来比较日期/时间

Example

例子

$start = new Carbon\Carbon('2014-01-05 12:00:00');
$end = new Carbon\Carbon('2014-01-01 12:00:00');
echo $end->diffInDays($start);

Or you can directly compare date from db

或者您可以直接比较数据库中的日期

$customer =App\Customer::first();
$start = $customer->db_date_column;
$end = new Carbon\Carbon('2014-01-01 12:00:00');
echo $end->diffInDays($start);

More information http://carbon.nesbot.com/docs/#api-difference

更多信息http://carbon.nesbot.com/docs/#api-difference

Updateif you want the time diff in hour, you can use this $end-diffInHours($start);instead.

如果您想要以小时为单位的时间差异,请更新,您可以改用它$end-diffInHours($start);

回答by Alireza Rahmani Khalili

You can use this sample:

您可以使用此示例:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

in Laravel just donot forget to add this:

在 Laravel 中不要忘记添加以下内容:

use DateTime;

the other option is you can use SQL, look at my sample:

另一种选择是您可以使用 SQL,请查看我的示例:

      return DB::table('corporation')->join('corporation_subscription','corporation.id','=','corporation_subscription.corp_id')
        ->where('corporation.id','=',$id)
        ->where('corporation_subscription.period','>=',DB::raw('DATEDIFF(NOW(),corporation_subscription.created_at)'))
        ->where('corporation_subscription.status','!=', 'cancel' )
        ->get();

this will help you too : DB::raw('DATEDIFF(NOW(),corporation_subscription.created_at)'))

这也会帮助你: DB::raw('DATEDIFF(NOW(),corporation_subscription.created_at)'))