在Laravel查询中如何将分钟添加到毫秒,并应用where条件?

huangapple go评论54阅读模式
英文:

how to add minutes to milliseconds in laravel query, and apply where condition?

问题

我有一个名为activities的表,其中waiting_time以分钟为单位,start_time以毫秒为单位。

Name   | event_start_waiting_time | event_time |
running| 30                       |1685944961000  |
walking| 20                       | 1686481069000 |

例如,event_time是下午5:00,event_start_waiting_time是30分钟,也就是说,我可以在下午5:00至5:30之间随时开始活动。我想根据我的当前时间获取这些活动。如果当前时间在任何活动范围内,就获取该活动。

$currentTimestamp = time() * 1000; 
$activities = Activity::where('event_start_waiting_time', '<=', $currentTimestamp)
    ->whereRaw('(event_time + (event_start_waiting_time * 60 * 1000)) >= ?', [$currentTimestamp])
    ->get();

我尝试了这个查询,但结果为空。

英文:

I have a table named activities, where waiting_time is in minutes and start_time is in milliseconds.

Name   | event_start_waiting_time | event_time |
running| 30                       |1685944961000  |
walking| 20                       | 1686481069000 |

for eg. event_time is 5:00 PM and event_start_waiting_time is 30 minutes i.e.. I can start activity anytime between 5:00 PM - 5:30 PM. I want to fetch those activities according to my current time. if current time lies between any of the activity range, fetch that activity.

$currentTimestamp = time() * 1000; 
$activities = Activity::where(&#39;event_start_waiting_time&#39;, &#39;&lt;=&#39;, $currentTimestamp)
    -&gt;whereRaw(&#39;(event_time + (event_start_waiting_time * 60 * 1000)) &gt;= ?&#39;, [$currentTimestamp])
    -&gt;get();

I have tried this query, but getting the empty result.

答案1

得分: 0

这是一个例子,

$minutes = 5; // 要添加的分钟数
$milliseconds = $minutes * 60 * 1000; // 将分钟转换为毫秒

$results = DB::table('your_table')
    ->whereRaw('your_column + ? <= UNIX_TIMESTAMP()', [$milliseconds])
    ->get();
英文:

Here is an example,

$minutes = 5; // The number of minutes to add
$milliseconds = $minutes * 60 * 1000; // Convert minutes to milliseconds

$results = DB::table(&#39;your_table&#39;)
    -&gt;whereRaw(&#39;your_column + ? &lt;= UNIX_TIMESTAMP()&#39;, [$milliseconds])
    -&gt;get();

huangapple
  • 本文由 发表于 2023年6月11日 20:12:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定