Laravel: DATE_ADD in where clause [duplicate] - php

This question already has answers here:
Is there a way to subtract an amount of days from a date in SQL?
(4 answers)
Closed 7 months ago.
I want to subtract days from the db column and then compare it with the current date using DATE_ADD function. Is this possible?
$valid_status_query->whereRaw('DATE_ADD("d",-3, "date_to")', '<=', Carbon::now()->format('Y-m-d'));
For example, if the column date is 2022-07-20 then it should use 2022-07-17 to compare in where clause at the end.

Use it like this:
$valid_status_query->whereRaw("DATE_SUB(date_to, INTERVAL 3 DAY) <= ?", [Carbon::now()->toDateString()]);

Related

Check if datetime is older than 5 days [duplicate]

This question already has answers here:
SELECT all records that are 30 days old
(4 answers)
Closed 1 year ago.
So I am going to make a cron job in cPanel that runs every day, but not sure how the query should look like. I have a datetime column with this type of value: 2021-04-06 14:12:06. How can select from table where datetime column is older than 5 days?
You can use date arithmetic. Assuming you mean "day" without the current time value:
where datetime_col < curdate() - interval 5 day
If you want the reference to be the current time rather than the current date:
where datetime_col < now() - interval 5 day

How to get date & month using date in mysql [duplicate]

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

Getting the difference between two MYSQL datetime columns in seconds [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MYSQL - datetime to seconds
If I have a datetime column in a mysql table, how can I get the difference between NOW() and that column in seconds?
by TIMEDIFF you can get the differense by seconds
SELECT TIME_TO_SEC(TIMEDIFF('2007-01-09 10:24:46','2007-01-09 10:23:46'));
or this
SELECT DATEDIFF('2007-12-31 10:02:00','2007-12-30 12:01:01') * 24*60*60;
// result: 86400 the difference in seconds for days.
or:
SELECT TIMESTAMPDIFF(SECOND,'2007-12-30 12:01:01','2007-12-31 10:02:00');
// result: 79259 the difference in seconds with the time.
Try this: SELECT NOW() - my_col AS my_diff

PHP Date compare with sql [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to use DATE FORMAT in my query?
I have a date from form post as 09-23-2012 and in my sql database i save date with datetime format like 2012-09-23 23:11:13
I want to create a query to compare these two date.
These two date should equal.
Try DATE_FORMAT:
WHERE DATE_FORMAT(the_field, '%Y-%m-%d') = CAST(:yourInput AS DATE)
Or something similar

Time Difference [duplicate]

This question already has answers here:
Timestamp Difference In Hours for PostgreSQL
(9 answers)
Closed 8 years ago.
I am new to PostgresQL and PHP and am working with a PostgresQL Timestamp object and am trying to find the difference between now and that timestamp to display in terms of years, months, and days. Is there to do this?
Thanks!
You can just subtract two timestamps, the result is an interval.
To get "now" you can use now() or current_timestamp (among others).
SELECT now() - '2010-02-21 20:11:32';
This will display something like this, though:
830 days 23:00:50.127241
To get a justified representation, use age() or justify_interval()
SELECT justify_interval(now() - '2010-02-21 20:11:32');
Displays the same value in a format like you seem to be after:
2 years 3 mons 20 days 23:01:34.095813
If you want a particular output format use to_char()
select age(now(), '2010-01-02 12:34:35');
The complete version:
select substring(a from 1 for (position('days' in a) + 3))
from (select (age(now(), '2010-01-02 12:34:35'))::text) s(a)
;
substring
------------------------
2 years 4 mons 30 days
(1 row)

Categories