简体   繁体   English

获取每行两个日期之间的差

[英]Get the difference between two dates in every rows

I need to get the difference between 2 dates but not only between 2 specific dates, I am talking about a table that has the "pdt_expires" column, and I want to show how much days until this date, but in EVERY rows. 我需要得到2个日期之间的差值,而不仅是2个特定日期之间的差值,我所说的是一个包含“ pdt_expires”列的表,我想显示直到此日期为止的天数,但要在每行中显示。 Ex: 例如:

  • Rice - 2 lb - expires in 54 days 大米-2磅-有效期54天
  • Beans - 3 lb - expires in 31 days 豆-3磅-31天后过期

I saw THOUSAND of solutions for only 2 specific dates but none for this case. 我只看到2个特定日期的数千个解决方案,但在这种情况下却没有。 I wonder I would need: 我想知道我是否需要:

  • a function in Product Model that access the "pdt_expire" and use Carbon to get the final value 产品模型中的一个函数,该函数访问“ pdt_expire”并使用Carbon获得最终值

OR - a function inside the View that get the $product->pdt_expire and get the final value 或-视图内的一个函数,该函数获取$ product-> pdt_expire并获取最终值

So any help would be great, thank you. 因此,任何帮助都会很棒,谢谢。

How about achieve it using just simple DATEDIFF query: 仅使用简单的DATEDIFF查询如何实现它:

ProductModel::select([
    'products.*',
    \DB::raw('DATEDIFF(pdt_expires, NOW()) as expires_in')
])->get();

So in every product you have, it will have an additional column called expires_in that stores different of days between NOW to pdt_expires . 因此,在您拥有的每个产品中,都会有一个名为expires_in的附加列,该列存储从NOWpdt_expires之间的不同日期。

DATEDIFF: How to get the number of days of difference between two dates on mysql? DATEDIFF: 如何获取mysql上两个日期之间的差异天数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM