This question already has answers here:
How to select year and month from the created_at attributes of database table in laravel 5.1?
(3 answers)
Closed 2 years ago.
I have the field date_start field which is a date datatype. I want to get the month and year of the field date_start and check the month is in given date month '2021-02-01'.
$meeting_ids=DB::table('meeting_dump')
->Where(DB::raw(date('m-y',strtotime('meeting_dump.date_start'))),'=',date("m-y", strtotime($dates_between[0])))
->pluck('meeting_dump.parent_id')
->toArray();
given date is 2021-02-01 and meeting start date is 2021-02-01, 2021-01-05 need to pick the parent_id for 2021-02-01
You can do it with Carbon:
(new Carbon('2021-02-01'))->format("m-y") //this will yield 02-21
You can do the same for your date field.
For your problem I would suggest to create date range of month start and month end from your input date instead of formatting the dates on fly
$date = \Carbon\Carbon::parse("2021-02-01");
$start = $date->startOfMonth()->format('Y-m-d H:i:s');
$end = $date->endOfMonth()->format('Y-m-d H:i:s');
And in query builder use between filter the benefit of this approach is your query remains sargable means if there is an index added on this column it will use that index but if you perform formatting on your date column then index will get ignored
$meeting_ids= DB::table('meeting_dump')
->whereBetween('date_start', [$start, $end])
->pluck('parent_id')
->toArray();
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Query to convert from datetime to date mysql
(5 answers)
Parse date in MySQL
(2 answers)
Closed 4 years ago.
I have inserte date into my table and it is in the format YYYY-MM-DD, However when displaying this information I would like to display the date the in DD-MM-YYY format. If anyone has an idea how to achieve this please let me know. Please see my sql query below as this query it not working.
// Create query using SQL string
$sql_query = "SELECT title, level, DATE_FORMAT(dateTo, '%W %M %e
%Y')
FROM jobPost ORDER BY jobID DESC";
// Query database using connection
$result = $conn->query($sql_query);
You can use the following:
DATE_FORMAT(dateTo, '%d-%m-%Y')
/* For dateTo value of 2018-11-13, it will output 13-11-2018 */
Details:
%d Day of the month as a numeric value (01 to 31)
%m Month name as a numeric value (00 to 12)
%Y Year as a numeric, 4-digit value
Check the complete list of available format specifiers at: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
This question already has answers here:
Compare only day and month with date field in mysql
(5 answers)
Get month name from Date
(40 answers)
Closed 5 years ago.
I want to get date & month from date. example 2017-06-15 so I want date & month i.e. -06-15. Do u have any idea? Really appreciated.
From Below query I am getting month from date. but I want both date & month
SELECT MONTHNAME(`date`) AS month_name FROM table_name;
SELECT DATE_FORMAT(date, '-%m-%d') AS month_name FROM table_name;
see the date_format documentation to see all the possible formats
MySQL MONTH() returns the MONTH for the date within a range of 1 to 12
( January to December). It Returns 0 when MONTH part for the date is
0.
SELECT MONTH('2009-05-18');
MySQL DAY() returns the day of the month for a specified date. The day
returned will be within the range of 1 to 31. If the given date is
‘0000-00-00’, the function will return 0. The DAYOFMONTH() is the
synonym of DAY().
SELECT DAY('2008-05-15');
Link : here
select month('2017-06-15'),DAY('2017-06-15')
you can use this function
DATEPART(datepart,date)
use below query
SELECT DATEPART(yyyy,datecolumn) AS dateYear,
DATEPART(mm,datecolumn) AS dateMonth,
DATEPART(dd,datecolumn) AS dateDay
FROM table_name
I already have SQL table with date column mentioned below.
SL.no Date name
1 15-02-2017 krish
I want to automatically update column date to one week when the current date is equal to table column date.
Example
Today date is 15-02-2017
My SQL column date is 15-02-2017
Then i want to update this automatically to 1 week when i access my home page.
Is this possible to do?
I hope you understand my questions.
Thanks in advance.
Looking to your example data your date field is a VARCHAR or CHAR type.
You need to use STR_TO_DATE to make it possible to use + INTERVAL 1 WEEK and DATE_FORMAT to convert back to your CHAR or VARCHAR field.
UPDATE
[table]
SET Date = DATE_FORMAT(STR_TO_DATE('15-02-2017', '%d-%m-%Y') + INTERVAL 1 WEEK, '%d-%m-%Y')
WHERE
Date = '15-02-2017'
This question already has answers here:
Get closest date from MySQL table
(3 answers)
Closed 7 years ago.
Hi i have some examples dates given below.How to find the next closest date in php or in mysql ?
I have some date like 12-04-2015 so now i need to get the closest date after the given date.So in my case my closest date is 15-04-2015.How can i find using PHP and mysql ?? Any one help me
PHP
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
Mysql
SELECT Birthdate FROM hedging ORDER BY ABS(DATEDIFF(Birthdate , `2015-04-12`)) LIMIT 1
How can i execute the above query in mysql ?
BirthDate
25-03-2015
10-04-2015
10-04-2015
11-04-2015
15-04-2015
30-04-2015
You've not given us much information about what you've tried.
In PHP, store the dates in an array, you can then simply sort it and choose the next value.
In SQL so long as they are stored as dates, then WHERE date > $date ORDER BY date DESC LIMIT 1 will give you the next date.
I have mysql date format m/d/y as varchar like 03/12/2015 for 12 March 2015. Now I want to count all the result from mysql table in which the month & year are the same as of today's date.
I am using following query, but no sucess
SELECT * FROM student where DATE_FORMAT(regd_date,'%m/%y')=DATE_FORMAT(now(),'%m/%y')"
Thanks for any help
You will first need to use STR_TO_DATE() to convert your varchar column to a date before using DATE_FORMAT().
SELECT * FROM student
WHERE DATE_FORMAT(STR_TO_DATE(regd_date, '%m/%d/%Y'),'%m/%y') = DATE_FORMAT(now(),'%m/%y')
Adjust the format for STR_TO_DATE() accordingly.
Note: I would discourage storing dates as varchars.