I have a model for which I have a database field called day. This is a unique field and it can contain a day like monday, tuesday, and so on. So monday can only be used once, etc. The value is literally stored as a string.
Now I am trying to display only upcoming x results, for example, only the results from the next 3 days. Which means, I want to display the result of today, tomorrow and the day after tomorrow. I have been trying like below:
Model::where('day', '>=', Carbon::now())
->where('day', '<', Carbon::now()->addDays(3))
->get();
But this does not give me any results. I want to prevent having to change the field to a date field, for now. But I was wondering if there is any way to achieve what I am looking for with a simple eloquent query.
if the day is unique you can get the next three days and select their results:
$today=Carbon::now()->dayName;
$tomorrow=Carbon::tomorrow()->dayName;
$afterTomorrow=Carbon::tomorrow()->addDay()->dayName;
$acceptableDays=[$today,$tomorrow,$afterTomorrow];
Model::whereIn('day', $acceptableDays)
->get();
pay attention to letter case, Carbon::now()->dayName; will gave you the day name with first capital letter, you should convert it to match the way it stored in your db
Related
I want to select all the data on a database using laravel db query or Eloquent ORM. So i want all the data that are other than 3 days from the day they where created. Data 'A' is created on 15th i come in on 16th to check, i won't see Data 'A' but i may see others older than 3 days or equal to 3 days. If i come back on the 18th or 19th i should see Data 'A', obviously because it now older than 3 days or equal to.
so i wrote this code that is not working below
$users = DB::table('matched_users')->where( 'created_at', '>=',
Carbon::now()->subDays(3));
so can anyone correct this for me.
when i did that it work but it shows both someone that his date is just one day one. so is that not enough code
Data Name - created_at
ozil 21/04/2012 16:09:22
mark 21/04/2012 16:09:22
cyril 22/04/2012 16:19:21
so today is 25/04/2012 if run the query of run the above query as it is i get all the result back.
But if change the >= to <= the result is an empty collection
I guess you mean 'older than 3 days from the day they where created'. In this case the query should look like this:
DB::table('matched_users')->where('created_at', '<=', Carbon::now()->subDays(3));
$users = DB::table('matched_users')->whereDate( 'created_at', '<=', Carbon::now()->subDays(3))->get();
is the right way to write the code
I have a deliverables table and i want to get results where difference between current date time is 2 days from created date.
e.g today is 4-19-2017 i want to get all records where deadline is 4-21-2017 because here is difference of 2 days between 4-19-2017 and 4-21-2017 or also if 4-20-2017 it should also retrieve how ever if difference is more than 2 it should not be retrieve.
In short if Difference between current date and deadline is 2 days or less should be retrieve
I tried as
$deliverables_dead = DB::table('deliverables')->where('user_id' , Auth::user()->id)->where('deadline' , '>' , Carbon\Carbon::now())->get();
its get all records where is deadline greater than current time.
Please help how can i get answer of above bold statement......
Use whereBetween() and Carbon's addDays():
DB::table('deliverables')
->where('user_id', auth()->id())
->whereBetween('deadline' , [Carbon\Carbon::now(), Carbon\Carbon::now()->addDays(2)])
->get();
I'm pretty new to using Laravel, but I've managed to create a pretty basic API. I'm starting to try and add filters to the API, and the primary filter I need is to see if a date range overlaps another date range.
For example, here is a typical data object returned from my database:
{
id: 1,
name: 'This is the name',
start_date: '2017-01-03',
end_date: '2017-01-29'
}
The users would typically filter by a smaller date range. For example, give me results whose start and end range fall between 2017-01-28 and 2017-02-04.
So something like:
data start data end
|------------------------|
|----------------|
search start search end
Throughout my research, I can only find answers relating to searching if a single date falls in a range, not if any part of a range falls inside another range.
This is what I currently have:
$closures = Closures::where('start_date', '>=', $from)->where('end_date', '<=', $to)->get();
The problem with this approach is that it doesn't really account for partial overlaps. In the above example, my search date is indeed greater than or equal to the data's, but since the end date is actually greater than the data's end date, it will not be returned.
The obvious would be to make the second stipulation and orWhere, but then that returns literally all data with a less than date which does not work.
I basically need to check if any part of of my search range falls within the data's range. Is this even possible, if so, any ideas on how I could achieve this.
You would check to see if any of the 2 dates provided falls between a range in your data. I would use nested wheres to avoid any logic spill over.
$closures = Closures::where(function($query) use ($start) {
return $query->where('start_date', '<=', $start)->where('end_date', '>=', $start);
})
->orWhere(function($query) use ($end) {
return $query->where('start_date', '<=', $end)->where('end_date', '>=', $end);
})
->get();
I'm new to MySQL and PHP but was wondering if someone could help me with a little project I'm doing for my boss.
I have a SQL database (MyDB) and a table in there (mytable) with two columns - the first column (index) is an auto-incrementing integer from 1-10, the second column (date) has different dates and timestamps in the format of Year-month-day time 2013-04-12 1326
I'm trying to create a simple PHP page that first gets the current date (easy enough) then looks at the table and shows the number of rows that fall within yesterday's date. For example, if I have 3 rows with 2013-04-11 XXXX and 2 rows with 2013-04-12 XXXX (and today is the 12th April 2013) the page will display 3. (The time is not important but we can't remove it from the table as it's auto created by one of the other staff's programs and he refuses to change it).
So far I've got my php page, done a connection to the DB and defined two variables:
$startdate = date('Y'."-".'n'."-".'d'." "."0000");
$enddate = date('Y'."-".'n'."-".'d'." "."2359");
As the timestamp doesn't matter I've gone for the min/max possible on the variables. I realise this will only give the current date, trying to work out how to get it to display the previous day as the date in the variable.
Now I'm trying to create a sql query that will count the number of rows where the date field falls within the startdate and enddate variables (-1 day) but not too sure where to start or how this would look. I then need to output this as a variable in PHP so I can echo it later in the page.
Anyone able to point me in the right direction? Hope any of this makes sense.
You could write a query with no params to do this (if its always just yesterday).
SELECT * FROM <table>
WHERE DATE_FORMAT(<date column>,'%j-%Y') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 DAY), '%j-%Y');
Date functions in the where clause might not be super awesome performance wise
How do you sort data which was stored in a mysql database depending on the days of the week in which the data was submited ??
I basically want to create a diary which outputs information in each day of the week depending on what day it was posted by dates so,
Mon - Data in order of date
Tue -
Wed - e.t.c
Any code examples and information will be great, thanks.
You can do a
SELECT DAYOFWEEK(datehere) as dayofweek, datehere FROM something ORDER BY dayofweek, datehere;
You can use the DAYOFWEEK function to extract the day, and then sort on it just like any other data.
What kinf of data type is the column where you store the date submission?
It seems like you're asking for a basic SELECT statement?
SELECT some_column, another_colum FROM your_table ORDER BY your_date_column DESC
This assumes you actually have a column that logs the insertion timestamp.
If this answer is obnoxiously simplistic, please forgive me...and give us more details :)
Regards.
If your data is stored as a DATE or DATETIME field, use the DAYOFWEEK or DATE_FORMAT functions to turn it into day name for output, but continue to order by the DATE field
SELECT DATE_FORMAT(my_date_column, '%W') AS dayofweek
FROM my_table
ORDER BY my_date_column
Well, the sorting bit is easy, just sort on the column that represents the post's date. The grouping in days is something you can do in your code, since you need to check the date there anyway (for post-processing the actual output).
To put it this way, you can do a subselect to get the specific day of the week, but in your code you would have to check the day again to group posts per day. In that case it's better (and cleaner, since you're separating business logic from data) to do this in your code, something like this:
select all posts (within date range)
make an associative array, with the
days as keys, and posts (in new
arrays) as values
loop through the
days and then posts to output the
posts per day
SELECT *
FROM diary_entries
ORDER BY FIELD(DAYOFWEEK(created), '2, 3, 4, 5, 6, 7, 1'), created
DAYOFWEEK grabs day of the week number (1 = Sunday).
FIELD makes Monday first day of the week.
After sorting by day of week, then sorted by date created.