英文:
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('event_start_waiting_time', '<=', $currentTimestamp)
->whereRaw('(event_time + (event_start_waiting_time * 60 * 1000)) >= ?', [$currentTimestamp])
->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('your_table')
->whereRaw('your_column + ? <= UNIX_TIMESTAMP()', [$milliseconds])
->get();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论