简体   繁体   English

我怎样才能根据您的 current_date 获取两个 Datetime 之间的数据?

[英]How can i still get the data between two Datetime based on your current_date?

So i have data in my database which look like this.所以我的数据库中有看起来像这样的数据。

id | value|  current_date |            time           | group
1      0      2020-03-30        2020-03-30 19:00:00       2
2      5      2020-03-30        2020-03-30 20:00:00       2
3      10     2020-03-30        2020-03-30 21:00:00       2 
4      5      2020-03-30        2020-03-30 22:00:00       2
5      5      2020-03-30        2020-03-30 23:00:00       2
6      5      2020-03-30        2020-03-31 00:00:00       2
7      5      2020-03-30        2020-03-31 01:00:00       2
8      5      2020-03-30        2020-03-31 02:00:00       2
9      5      2020-03-30        2020-03-31 03:00:00       2
10     5      2020-03-30        2020-03-31 04:00:00       2
11     5      2020-03-30        2020-03-31 05:00:00       2
12     5      2020-03-30        2020-03-31 06:00:00       2
13     5      2020-03-30        2020-03-31 07:00:00       2
14     5      2020-03-30        2020-03-31 08:00:00       2
15     5      2020-03-30        2020-03-31 09:00:00       2
16     5      2020-03-30        2020-03-31 10:00:00       2
17     5      2020-03-30        2020-03-31 11:00:00       2

My goal is to get all data that is between 7PM - 12NOON.我的目标是获取晚上 7 点到中午 12 点之间的所有数据。

In my php code i am passing a parameter which is the current_date(Assume that today is March 30) for this day.在我的 php 代码中,我传递了一个参数,它是这一天的 current_date(假设今天是 3 月 30 日)。 So that i can put it on my where clause in my database query.这样我就可以将它放在我的数据库查询中的 where 子句中。

$shift_date = '';

$shift_date = ($shift_date != '') ?
            DateTime::createFromFormat('m-d-Y', $shift_date, new DateTimeZone('Asia/Manila')) :
            DateTime::createFromFormat('Y-m-d', date("Y-m-d"), new DateTimeZone('Asia/Manila'));

$current_hour = DateTime::createFromFormat('Y-m-d H:i:s', $shift_date->format('Y-m-d') .' 19:00:00', new DateTimeZone('Asia/Manila')); 

//2020-03-30 19:00:00

And this is my query, with this query i have no problem on getting the data because it is still March 30.这是我的查询,通过这个查询,我在获取数据方面没有问题,因为它仍然是 3 月 30 日。

SELECT value,time,current_date,group
FROM table
WHERE time >='2020-03-30 19:00:00' AND time <= DATE_ADD("2020-03-30 19:00:00", INTERVAL 16 HOUR)
AND group = 2;

The dilema that i am having is what if the date gets to 2020-03-31 :00:00:00 then this line of code in my php will have a value of 2020-03-31: 19:00:00 then i will not already get the data that i want.我遇到的困境是,如果日期到达2020-03-31 :00:00:00那么我的 php 中的这行代码的值为2020-03-31: 19:00:00然后我不会已经得到我想要的数据。

$shift_date = '';

$shift_date = ($shift_date != '') ?
            DateTime::createFromFormat('m-d-Y', $shift_date, new DateTimeZone('Asia/Manila')) :
            DateTime::createFromFormat('Y-m-d', date("Y-m-d"), new DateTimeZone('Asia/Manila'));

$current_hour = DateTime::createFromFormat('Y-m-d H:i:s', $shift_date->format('Y-m-d') .' 19:00:00', new DateTimeZone('Asia/Manila')); 

//2020-03-31 19:00:00

I should still get the data that i need between the March 30 7PM - March 31 11 noon.我仍然应该在 3 月 30 日晚上 7 点到 3 月 31 日中午 11 点之间获得我需要的数据。 I am really stuck with this problem that i have.我真的被我遇到的这个问题困住了。 Thank you for all the advance help that will give me time to help me on this.感谢您的所有预先帮助,这将使我有时间在这方面帮助我。

Hi i know my question is really confusing, but i already solve it.嗨,我知道我的问题真的很令人困惑,但我已经解决了。

the end time is always 11PM so i just need to -12 hours so i can fetch all data base on the current_date in my database;结束时间总是晚上 11 点,所以我只需要 -12 小时,这样我就可以根据数据库中的current_date获取所有数据;

$shift_date = date_create(date("Y-m-d"));
$shift_date->modify("-12 hours");

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

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