How To Add Interval 30 Days in Laravel (MYSQL) - php

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();

Related

how to convert core query of sql in laravel?

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)']);

Laravel 4 Query with 30 Days Ago Condition

I dont understand where goes wrong. When I add this
where('units.solddate','>=','DATE_SUB(CURDATE(), INTERVAL 30 DAY)')
where clause and my result is empty. I tried in phpMyadmin and it does return results.
$query= (tables)
->select(DB::raw('SUM(units.price) as price, DATE(units.solddate) as date, DAY(units.solddate) as day'))
->where('units.solddate','>=','DATE_SUB(CURDATE(), INTERVAL 30 DAY)')
->where('units.solddate','<=','NOW()')
->groupBy('date')
->get();
Please advice.
The reason why the constraints don't work is that Eloquent takes the value you compare with literally, escapes it when needed and then uses in a query. So if you do
->where('units.solddate','<=','NOW()')
you're in fact comparing units.solddate with a strin NOW(), like in:
... WHERE units.solddate <= 'NOW()'
If you want to use MySQL functions in your query you have to explicitely tell Eloquent/QueryBuilder that you mean the raw value that you provided by using DB::raw() to wrap the value.
The following should work for you:
->where('units.solddate','>=',DB::raw('DATE_SUB(CURDATE(), INTERVAL 30 DAY)'))
->where('units.solddate','<=',DB::raw('NOW()'))

How to filter MySQL dates?

I'm trying to do a SELECT * FROM but only items that are less than 30 days old. Here is my select code:
SELECT * FROM `{$table_name33}` WHERE `type`='wpst-requiredinfo' ORDER BY `foreignkey` ASC;
However, my problem is that I can't figure out how to add WHERE AND last_updated is less than 30 days.
I'm not exactly sure how to write the query, but the date is showing up like this: 1428412603 in the table column, it doesn't look much like a date to me. I don't know where to start.
Try this where clause:
WHERE `type`='wpst-requiredinfo' and
last_updated >= date_sub(now(), interval 30 day)
EDIT:
Your date seems to be in Unix time format.
WHERE `type`='wpst-requiredinfo' and
last_updated >= unixtime_timestamp() - 30*24*60*60
Note: this puts all the functions on the current time. In particular, it does not use FROM_UNIXTIME(last_updated). This ensures that an index can be used for this part of the query. The best index would be on (type, last_updated).

PHP Simple Time Check (hour)

Although I have been working with PHP for a while, the one part of it I am still trying to get right is time.
I am creating a simple script that will check if the timestamp is greater than or equal to an hour, and if it is, it will be deleted from the database.
2013-01-03 20:30:25
DELETE FROM tablename WHERE timestamp = ?????
I am not sure how to execute the query to delete values with a timestamp of over an hour from the current time. Any help is greatly appreciated.
DELETE FROM tablename WHERE `timestmap` < DATE_SUB(NOW(), INTERVAL 1 HOUR)
Ref:- date_add and date_sub
First of all, 2013-01-03 20:30:25 is not a timestamp, it is a formatted date. The timestamp for that date would look like this: 1357245025. You can convert it to a timestamp using the strtotime function. You can also work out the timestamp of an hour ago by using strtotime("-1 hour") and performing a comparison on the values.
It might be faster just to do all of this within the MySQL query though, MySQL provides queries to do this, using a query similar to the one that Amit Garg provided.

Select Before The Date

I have a date, say its called $date. I want a a mysql_query to search a select number of weeks,days or even months before my $date. Is this possible? My explanation is not the greatest, but I do need a answer for this and do not know how to properly question it.
You could use mysql interval function?
"select * from table where `date` BETWEEN DATE_SUB(".$date.",INTERVAL 15 DAY ) AND CURDATE( )
That'll return the records from the last 15 days, you could use = insted of between if you want the records exactly 15 days old, or modify it for days, months, etc.
edit: if your working with php's time() remeber to use FROM_UNIXTIME($phpdate) inside your query.
i have a solution for this in SQL, Take it, if it would helps you
Day($date) gives you the date in the vaariable
Month($date) gives you the Month in the vaariable
Year($date) gives you the year in the vaariable
using simple where conditions, now you can search for a particulars
You can use the DATE_ADD and DATE_SUB functions to modify a date, and mysql understands a BETWEEN clause using dates. However, you can also use the TIMESTAMPDIFF function like so:
"SELECT foo FROM table WHERE TIMESTAMPDIFF(DAY, dateField, '$date') < '$desired_days'"

Categories