I have a date in the datatabase like this: 2019-08-28
I need to make a query where I can select the rows between two dates BUT just filtering the month and the day. I have this:
$session_data = $request->session()->all();
$date1 = date('Y-m-d');
$date2 = date('Y-m-d', strtotime($date1. ' + 4 days'));
$employees = Employee::whereBetween('born_date', array($date1, $date2))->take(4)->get();
but this selects does not work because it is filtering with the year..i need something which it uses this:
'DAY(born_date) = '. date('d')
'MONTH(born_date) = '. date('m'))
But I do not know how to add this with between.. Thanks
Laravel has helpful whereXYZ() methods to filter by day/month/year/time.
You can filter by a given month:
/*
|
| Get all the employees that were born between
| june and december of any year in your db
|
*/
$employees = Employee::whereMonth('born_date', '>=', 6)->get();
or make a filter by day:
/*
|
| Get all the employees that were born between the
| 1st and the 10th of every month of every year
|
*/
$employees = Employee::whereDay('born_date', '=<', 10)->get();
Of course you can combine this methods to accomplish your desired output.
Try the code below
$employees = Employee::whereRaw("DATE_FORMAT(born_date, '%m%d') BETWEEN DATE_FORMAT(NOW(), '%m%d') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 4 DAY), '%m%d')")->take(4)->get();
The idea is to format dates as %m%d and compare these values
Related
I have call_logs and jobseekers tables.Both tables have created_at column.And there is jobseeker_id in call_logs table.
In Jobseeker.php
public function calllogs()
{
return $this->hasMany('App\Calllog','jobseeker_id');
}
In Calllog.php
public function jobseeker()
{
return $this->belongsTo('App\Jobseeker','jobseeker_id');
}
I want to show a table like
created_at call_logs jobseekers
----------------------------------- ---------------------------------
2017-08-05 1 3
2017-08-04 2 2
2017-08-03 3 1
2017-08-02 2 4
2017-08-01 5 8
So, I can track the record, how many call_logs and how many new jobseekers in each day.
Here is how i tried in one table, its work well.But I want to make two tables at once query,
$count = DB::table('calllogs')
->select('created_at', DB::raw('count(*) as calllogs'))
->get();
it can show me, like
created_at calllogs
----------------------------------- ----------
2017-08-03 2
2017-08-04 3
2017-08-05 8
2017-08-06 6
So please guide me a way to do ? Should I use union? Thanks in advance.
There are 2 ways how to approach this. Both require a bit of coding.
Firstly you make 2 different queries, for example make this query as well.
$count2 = DB::table('jobseekers')
->select(DB::raw('DATE(created_at) as created_at'), DB::raw('count(*) as jobseekers'))
->groupBy(created_at'))
->get();
Now you align the counts by day and show it in the table.
Other option is to make query for each day and count entries for calllogs and jobseekers on that day.
$start_date = Carbon::now()->subWeek();
$end_date = Carbon::now()->now();
$dates = [];
for ( $date = $start_date; $date->lte( $end_date ); $date->addDay() ) {
$dates[] = $date);
}
$counts = [];
foreach ( $dates as $date ) {
$dateLater = $date->copy()->addDay();
$callLogs = Calllog::whereBetween( [$date, $dateLater] )->count();
$jobSeekers = Jobseeker::whereBetween( [$date, $dateLater] )->count();
$counts[ $date->format( 'Y-m-d' ) ] = [ $callLogs, $jobSeekers ];
}
Here $counts has count of job seekers asd calllogs for every day in range.
I have my date in format March 2014. How do I get month and year in number format to compare it in Oracle 10g query.
<?php
$date = "March 2014";
?>
My query that I am trying to get is
select EMP_NAME where month = "03" and year = "2014".
Here is my table structure
EMP_ID | EMP_NAME | C_DATE
1 | ABC | 01-FEB-14
2 | XYZ | 03-MAR-14
Note: I am writing the coding in php
For a sargable predicate you should avoid altering every row of data to suit the single parameter.
Instead: Adjust the parameter to suit the data
select
*
from your_table
where C_DATE>= to_date('March 2014','MON YYYY', 'NLS_DATE_LANGUAGE = American')
and C_DATE< add_months(to_date('March 2014','MON YYYY', 'NLS_DATE_LANGUAGE = American'),1)
Note the data - C_DATE - is not affected by any function, yet you get the desired outcome (all records of March 2014); this method permits use of indexes on the field C_DATE for query efficiency.
You can try with TO_CHAR(date, 'MON') for the months and TO_CHAR(date, 'YYYY') for the year.
The query will be
select EMP_NAME where TO_CHAR(C_DATE, 'MON') = month and TO_CHAR(C_DATE, 'YYYY') = year
And in php you'll have to manage the date to get year and month and pass them to the query
$dt = new DateTime("March 2014");
$month = $dt->format("m"); //03
$year = $dt->format("Y"); //2014
//Here connect to the db..
$db->query("SELECT EMP_NAME FROM yourTable WHERE TO_CHAR(C_DATE, 'MON') = :month AND TO_CHAR(C_DATE, 'YYYY') = :year");
$db->bindParam(":month", $month);
$db->bindParam(":year", $month);
I need to create something to display an image based on the current date and time. I have an SQL table set up that looks like this:
eventid | startdate | enddate | imgfile | status
6 | 2013-04-29 | 2013-05-03 | finals.jpg | active
What I need to do is compare the start date and end date to the current date, and if the current date falls within the startdate and enddate, then display the imgfile.
I'm not sure how to go about this, but this is what I tried.
//get current date and time
$currentdatetime = strtotime(now);
$formatteddatetime = date("y-m-d", $currentdatetime);
while($row=mysql_fetch_array($getimage))
{
//get variable for startdate
$formattedstartdate = date("y-m-d", $row[startdate]);
$formattedenddate = date("y-m-d", $row[enddate]);
if($formatteddatetime >= $formattedstartdate AND $formatteddatetime <= $formattedenddate AND $row[status] == 'active')
{
echo $row[imgfile];
}
}
?>
Right now, it doesn't show anything inside of the entire while statement. Am I at least on the right track?
the CURRENT_DATE returns the current date in ‘YYYY-MM-DD’ format or YYYYMMDD format depending on whether numeric or string is used in the function. CURDATE() and CURRENT_DATE() are the synonym of CURRENT_DATE.
You should do it in your SQL query instead of PHP. For example:
SELECT * FROM table WHERE CURRENT_DATE BETWEEN startdate AND enddate;
CURRENT_DATE() is a MySQL function that returns the current date, as the name suggests. It is one of a few special functions that have an alias without the parentheses.
I have a database holding two bits of information. from date to date. I need this entry to appear in my calender on every day, but I can only manage to get to appear on the first and last date.
example of DB:
job_no | date1(from)| date2(to)
________________________________
1 |2013-01-28 | 2013-02-03
2 |2013-01-14 | 2013-01-18
Edit for question. the search bar I have allows for ONE date input and the the calender finds entries through date1 and the next 6 days.
I cannot have a search which contains two date inputs because my users are so used to this way and i do not want to increase searching time. I started to think that I had to find the dates between the dates, add that to an array then use an if statement to find matches...but even saying this makes no sense to me.
regarding job 1, I need my calender to show this job up on all dates 28/29/30/31/01/02/03.
My current search SELECT * FROM jobs WHERE date1='$searchinput' PER day and calender this search. I use strtotime to increase the input date by +1 to add to the search for each day.
Calender page.
What I want with my results. User searched Date 28th.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | job no 1 | job no 1 ...... | job no 1
What I have now.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | blank | blank | job no 1
each day has a new select query right now. It matches days with date 1 and date 2. I dont need this as before I only had jobs out on one day now they go out for more than one day and need the job to be noted on all days it is out by only using a job from date and job to date.
EDIT 2:
SELECT * FROM calender_db NATURAL JOIN job_db
WHERE section='two'
AND date1 < '$day' AND date2 > '$day'
OR date1 = '$day' OR date2 = '$day'
This query selects what I need, but as I am using OR the first WHERE CLAUSE can be null. I need that to always be in the clause. I have been looking at using IIF or CASE to rectify but do not how to implement 100%...?
why not use BETWEEN
SELECT * FROM tableName WHERE date BETWEEN 'date1' AND 'date2'
SQLFiddle Demo
UPDATE 1
SELECT *
FROM tableName
WHERE date BETWEEN '2013-01-28' AND '2013-01-28' + INTERVAL 6 DAY
SQLFiddle Demo
To generate a list of days between two dates:
$days = array();
$stop = strtotime($date2);
for ($current = strtotime($date1); $current <= $stop; $current = strtotime('+1 days', $current)) {
$days[] = date('d', $current);
}
echo join('/', $days);
Demo
Update
Misunderstood the question it seems, if both dates are stored as columns and you're querying with a single date:
SELECT *
FROM jobs
WHERE 'date_from_input' BETWEEN date1 AND date2
Update 2
Your latest query can be written as:
SELECT *
FROM calender_db NATURAL JOIN job_db
WHERE section='two' AND '$day' BETWEEN date1 AND date2
Try like this
SELECT * FROM Mytimetable WHERE date BETWEEN 'date1' AND 'date2'
Query between date1 - date2
SELECT *
FROM `objects`
WHERE (date_field BETWEEN '2013-01-30 14:15:55' AND '2013-02-19 10:15:55')
OR
SELECT *
FROM `objects`
WHERE (date_field BETWEEN date1 AND date2)
query to select dates between two dates with PHP variables.
i will explain with exapmle :
$date1 = "2013-02-19";
$date2 = "2013-02-25";
then sql query will be :
$query = " SELECT * FROM table_xyz where (date_item BETWEEN date('".$date1."') AND date('".$date2."')";
hope it solves your problem
SELECT
*
FROM
tablename
WHERE
date between '$date1' and '$date2'
order by
date
hi im trying to get monthly sale of a online shop for a given period of time
i'm using unix epoch(time()) for storing sale date in the table
sold table looks like this
+-----------+---------------------+---------+----------+
| id | product_id | price | date |
+-----------+---------------------+---------+----------+
| 1 | 20 | 2000 |1323299365|
+-----------+---------------------+---------+----------+
| 2 | 28 | 3500 |1323297389|
+-----------+---------------------+---------+----------+
i want to findout monthly sale for a given period of time like
$from="3/2011";
$to ="4/2011";
i know how to change these dates to unix timestamp and get the result by something like that
//explode from
//$from = maketime(x,x,x,x,x);
//explode to
//$to= maketime(x,x,x,x,x);
$query = "select * from `sold_table` where date > $from and `date` < $to ";
but what i want is to group the results by month so i can have monthly statics
i know this query doesn't work but i want something like this
$query = "select sum(`price of each month`) from `sold_table` where date > $from and `date` < $to group by month ";
so i can have a result like
march(03) = 25000$;
april(04) = 200$ ;
i can group the result by every 30 days but i think there should be a cleaner way
maybe i can find out month between to epoch time like 1323299365 and 1323297389 and then get the monthly sale for each one of month ? but i dont know ho to extract month between two epoch time
Try something like this:
SELECT SUM(price), DATE_FORMAT(FROM_UNIXTIME(date), "%m") AS month FROM sold_table GROUP BY month
But be aware also of the year, you can use combination of month and year.