i am using laravel 5.2 framework. I want to find the all events that happens in next seven days. Here i have successfully find the core query but i want to convert with laravel query Can anyone help me
Here is my query
SELECT * FROM `allocations` WHERE `date` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY) ;
Can anyone help me. Thanks in advance :)
Try this:
Allocation::where('date', '>', Carbon::now())
->where('date', '<', Carbon::now()->addWeek())
->get();
update
I'm not sure if this will also work but please try;
Allocation::whereBetween('date', [Carbon::now(), Carbon::now()->addWeek()])->get();
Simple:
DB::select("SELECT * FROM `allocations` WHERE `date` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)");
A little more complicated:
DB::table('allocations')->whereRaw("`date` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)")->get();
If you have an Eloquent model set up, then replace DB::table()-> with your model name:
Allocations::whereRaw("`date` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)")->get();
Using Eloquent Models:
use App\Allocation; // Assuming this is your Eloquent model
use DB;
Allocation::whereBetween('date', [DB::raw('NOW()'),
DB::raw('DATE_ADD(NOW(), INTERVAL 7 DAY)']);
Using DB Facade
use DB;
DB::table('allocations')->whereBetween('date', [DB::raw('NOW()'),
DB::raw('DATE_ADD(NOW(), INTERVAL 7 DAY)']);
Related
Hi I got stack when i want to show data using interval in Laravel, I have this code and it's running well on Mysql and showing what i want. but how to implement the mysql code to laravel ?
this is mySql code
SELECT * FROM projects WHERE end_date BETWEEN CURRENT_DATE() AND DATE_ADD(CURRENT_DATE(), INTERVAL 30 DAY)
and i have trying some ways on Laravel Controller but array is null
$highlightProject = project::select('*')->where(DB::raw('end_date BETWEEN CURRENT_DATE() AND DATE_ADD(CURRENT_DATE(), INTERVAL 30 DAY)'))->get();
dd($highlightProject);
The reason you're not getting the results you want is because you're using where instead of whereRaw. In this case your query going to return:
select * from `projects`
where end_date BETWEEN
CURRENT_DATE() AND DATE_ADD(CURRENT_DATE(), INTERVAL 30 DAY) is null
(notice the is null part).
To get the results you want change where to whereRaw (you can remove the DB::raw):
project::whereRaw('end_date BETWEEN CURRENT_DATE() AND DATE_ADD(CURRENT_DATE(), INTERVAL 30 DAY)')->get();
Alternatively, you could use a mixture of whereBetween and Carbon:
project::whereBetween('end_date', [now(), now()->addDays(30)])->get();
Check out this answer for more information.
This may not be the solution you would have preferred, (I do not know why your current query would not work and as #tadman suggested, I too suggest checking the produced query), but an alternative approach would be to try with query builder methods and Carbon date library that is shipped with Laravel.
$highlightProject = Project::whereBetween('end_date', [\Carbon\Carbon::today(), \Carbon\Carbon::today()->addDays(30)])->get();
Try with whereRaw() sometime where() not properly work DB::raw()
$highlightProject = project::select('*')->whereRaw(DB::raw('end_date BETWEEN CURRENT_DATE() AND DATE_ADD(CURRENT_DATE(), INTERVAL 30 DAY)'))->get();
I have a mysql(i) databse that is written to every minute (usually 4 or 5 seconds after the minute).
I would like to select the values that are cleset to top-of-the-hour for the last 36 hours and I've no idea how to do it.
I've been playing with INTERVAL and DATE_ADD but have not found something that works yet. Any help would be appreciated.
Edit:
Extra info:
Table name:
temperature
Column names:
uid (AI)
time (timestamp)
probe0
probe1
probe2
probe3
probe4
Perhaps it would also be better to be
now
now -1hour
now -2hours
etc
now -36hours
FWIW, I'm currently using the following code so select ALL the data for the last 36 hours (2160 rows)
SELECT time, probe0, probe1, probe2, probe3, probe4 FROM temperature WHERE temperature.time >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE) ORDER BY temperature.time DESC
If you want to get the first record for each hour you can use MySQL's MIN() function and since it is an Aggregate Function you need to use GROUP BY to group your data by date and hour. So a sample query will look like:
SELECT
MIN(`time`) as first_for_hour, probe0, probe1, probe2, probe3, probe4
FROM `temperature`
WHERE `time` >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE)
GROUP BY DATE(`time`), HOUR(`time`);
and for reading data 36 hours back I used your WHERE clause:
WHERE `time` >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE)
I hope this will be helpful to you and here is the playground.
I was working on something and I needed to query a record from the database that is exactly 5 days from now, but it did not work as expected.
I have tried something but it's just fetching record of from 5 days till now. All I wanted is the record of exactly 5 days past (or ago). Here is my code, please tell me want I am not doing right.
SELECT * FROM ts_user
WHERE user_registerdate >= DATE_SUB(CURDATE(), INTERVAL 5 day) AND user_registerdate <= CURDATE()
ORDER BY user_registerdate DESC;
If user_resgisterdate field from your database is DateTime
Try this
SELECT * FROM ts_user
WHERE Datediff(day,user_registerdate,getdate()) <= 5;
Hope this helps
This gets exactly 5 days ago,
SELECT *, DATE_FORMAT(user_registerdate, '%m/%d/%Y')
FROM ts_user
WHERE DATE(user_registerdate) = CURDATE() - INTERVAL 5 DAY;
I want to return records from database 15 days old from end _date to till end_date!
I am searching for the query for last 3 days!
However. I want your help to do a query. Its simple but I'm not sure how to do it.
I wrote query something like :
SELECT *
FROM bid_post
WHERE ending_date
BETWEEN DATE_SUB( DATE(`ending_date`) , INTERVAL 15
DAY )
AND ending_date >= CURDATE()
But it is not working !
The data column is a Varchar type. I am storing date as YYYY-mm-dd format
Does somebody now how can I accomplish this?
Thanks.
Please try with this query
SELECT *
FROM bid_post
WHERE ending_date between DATE_SUB( CURDATE() , INTERVAL 15 DAY )
AND CURDATE()
You should never store dates as varchar since these are not real dates and you need to store them using mysql native date data types.
Since the format is Y-m-d you may not need to do a conversion using str_to_date function and can use the query as
select * from bid_post
where
ending_date between date_sub(curdate(),interval 5 day) and curdate()
This will give you data from last 15 days till today.
Using conversion to real date you need str_to_date as
select * from bid_post
where
str_to_date(ending_date,'%Y-%m-%d') between date_sub(curdate(),interval 5 day) and curdate() ;
DEMO
I have a column in my database 'scheduledDate', I need all rows in the database that have a scheduledDate within 7 days from today.
I could use a query like this:
SELECT * FROM tblName WHERE `scheduledDate` > DATE_ADD(now(), INTERVAL 7 DAY
The only problem is, the 'scheduledDate' column is not formatted as a mysql timestamp (YYYY-MM-DD HH:MM:SS), but is formatted just as a standard American Date (07/09/2014). Is there a way to grab all rows within the next 7 days in my query? I was thinking that it might be possible using DATE_FORMAT, but I have been unable to figure it out.
You can convert the string to a date using str_to_date():
WHERE str_to_date(`scheduledDate`, '%m/%d/%Y') BETWEEN now() and DATE_ADD(now(), INTERVAL 7 DAY)
The logic also needs to change. The above uses between, but this may not be the logic your really need because of the extraneous time component on now(). Perhaps this is closer:
WHERE str_to_date(`scheduledDate`, '%m/%d/%Y') BETWEEN CURRENT_DATE() and DATE_ADD(CURRENT_DATE, INTERVAL 7 DAY)
WHERE scheduledDate >= Date_Add(now(), INTERVAL -7 DAY)