How can I get the difference of months between two dates in MySQL only if the days are the same? Here is what i have so far but it doesn't work because when the days are not the same, it still works:
TIMESTAMPDIFF(MONTH, '2019-05-05', '2019-06-05')
Output: 1
TIMESTAMPDIFF(MONTH, '2019-05-05', '2019-06-22')
Output: 1 // should NOT work because days are 05 and 22. Needs to be the same dates
I can obviously do:
DAY('2019-05-05') = DAY('2019-06-05') AND TIMESTAMPDIFF(MONTH, '2019-05-05', '2019-06-05')
but how can I do it in one function?
Thanks!
SUBDATE()/ADDDATE/DATE_SUB/DATE_ADD will handle this well:
SELECT DATE_SUB('2019-06-22', INTERVAL 1 MONTH) = '2019-05-05'
| DATE_SUB('2019-06-22', INTERVAL 1 MONTH) = '2019-05-05' |
| ------------------------------------------------------: |
| 0 |
SELECT DATE_SUB('2019-06-22', INTERVAL 1 MONTH) = '2019-05-22'
| DATE_SUB('2019-06-22', INTERVAL 1 MONTH) = '2019-05-22' |
| ------------------------------------------------------: |
| 1 |
See the DBfiddle
In MySQL, you can get it by using the below query...
select DATEDIFF('2019-06-22', '2019-05-05') as Diff from table_name where
day('2019-06-22') == day('2019-05-05');
using column name...
select DATEDIFF(dateColum2, dateColum1) as Diff from table_name where
day(dateColum2) == day(dateColum1);
From PHP you can get it like below...
$dateValue1 = '2019-05-05';
$dateValue2 = '2019-06-22';
$time1=strtotime($dateValue1);
$day1=date("D",$time1);
$time2=strtotime($dateValue2);
$day2=date("D",$time2);
if($day1 == $day2 ) {
$seconds_diff = $time2 - $time;
}
Related
Database table i have:
S.no | j_id |age | e_date |
-------------------------------------
1 | 1 |32 | 2018-05-09 |
-------------------------------------
-------------------------------------
1 | 1 |32 | 2018-05-09 |
-------------------------------------
-------------------------------------
1 | 2 |32 | 2018-05-09 |
-------------------------------------
-------------------------------------
1 | 2 |32 | 2018-04-16 |
-------------------------------------
-------------------------------------
1 | 1 |32 | 2018-09-16 |
-------------------------------------
-------------------------------------
1 | 3 |32 | 2018-04-16 |
------------------------------------
In my table I have expiry date I want to get the count of the result whose expiry date (90 days before) is equal to current date.
like I have expiry date 2018-05-09 and current date is 2018-02-2018 (90 days before date ) now i want to get to the count of the 90 days before result by query.
select * from yourtable
where datediff(CURDATE(), e_date) > 90
try to use datediff to get the different date count in day. Hope that this is what you want to get.
I like to keep the logic on the PHP side as much as possible, so I would probably calculate my expiry date in PHP and just add a simple where to the query. In Laravel that could look something like this:
$expireTreshold = Carbon::now()->addDays(90);
$expireCount = $myModel->where('e_date', '<=', $expireTreshold)->count();
For getting the current date, you can use CURDATE(), CURRENT_DATE(), and NOW() any one of these functions would get the current date. While the DATEDIFF() will get the difference between two periods (start date to end date).
If you only need to get the expiry dates that fit the 90 days condition use this :
SELECT e_date
FROM tableName
WHERE
e_date >= NOW()
AND datediff(e_date, NOW()) <= 90
In the query, you're scanning for future dates (from the current date and forward) and then get the differences, if the differences is less than or equal to 90 days, then it'll be selected.
if you need to show how many days left to each user:
SELECT sno, datediff(e_date, NOW())
FROM test
WHERE
e_date >= NOW()
AND datediff(e_date, NOW()) <= 90
It will work fine.
SELECT count(*) FROM table WHERE e_date < CURDATE() - INTERVAL 90 DAY;
OR
SELECT count(*) FROM table WHERE e_date < NOW() - INTERVAL 90 DAY;
cheers :)
Try this:
$your_date = "2018-01-01";
query = 'SELECT COUNT(*)
FROM thetable WHERE e_date =
DATE_ADD($your_date, INTERVAL 90 DAY)';
I've got a mysql-database like this:
ID | date | value
1 | 2017-02-20 | 200
2 | 2017-02-17 | 400
3 | 2017-02-15 | 630
4 | 2017-01-28 | 110
5 | 2017-01-29 | 220
I know how to get the sum of january, february, ....
SELECT SUM(value) AS wert
FROM table
WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 2 MONTH)
AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 2 MONTH)
But now I need a code to get the average of january and february.
I tried
SELECT SUM(value) AS wert, AVG(wert) as average
FROM table
WHERE YEAR (date) = YEAR(CURRENT_DATE - INTERVAL 2 MONTH)
AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 2 MONTH)
But this didn't work.
Thank you for your help!
WHERE date >= '2017-1-1' AND date < '2017-3-1'
Can anyone tell me how to delete items from a table with more than 5 days in CodeIgniter please? I searched here and on google and can't find anything that works.
EDIT:
My table in database looks like this:
| ID| Name | CreationDate |
| 1 | Test | 01-12-2014 |
| 2 | Test2| 01-11-2014 |
and so on, I hope you understand me.
Steps:
Get the current date
Calculate the date 5 days ago
Delete the items from the database where the CreationDate is less than (older than) 5 days
Code:
$date = date("Y-m-d H:i:s",time());
$date = strtotime($date);
$min_date = strtotime("-5 day", $date);
$this->db->where("CreationDate < '$min_date'", NULL, FALSE);
What is the type of CreationDate? What do you mean by more than 5 days?
If you mean you want to delete items where CreationDate is older than 5 days, and CreationDate type is string (varchar), you could use this:
$this->db->query("DELETE FROM MyTable where STR_TO_DATE(CreationDate, '%d-%m-%Y') < DATE_SUB(NOW(), INTERVAL 5 DAY)";
But if CreationDate type is mysql date, then simply use:
$this->db->query("DELETE FROM MyTable where CreationDate < DATE_SUB(NOW(), INTERVAL 5 DAY)";
I have a table with a date field (2013-07-11 = July 11th, 2013).
I need to select entries from the monday (the start of my week) of any given week until the end of that week.
I've seen queries that can grab rows whose date is 1 week ago, but I specifically need to grab rows that have a date field that occurs this week, regardless of what the current day of the week is.
I'm currently grabbing it in php using this, but I feel like there has to be a mysql method for calculating the beginning and end of this week:
$this_monday = date('Y-m-d', strtotime('previous monday'));
$this_friday = date('Y-m-d',strtotime('this friday'));
$sql = "SELECT `date`, $sign FROM `horoscopes`
WHERE `date` >= '$this_monday' AND `date` <= '$this_friday'
AND type = 'Daily'
ORDER BY `date` ASC
";
And I'm pretty sure that on monday it will end up grabbing monday of last week, not monday.
from this data set I'd like to select the following:
+------------+
| date |
+------------+
| 2013-07-12 |
| 2013-07-11 |
| 2013-07-10 |
| 2013-07-09 |
| 2013-07-17 |
| 2013-07-08 |
| 2013-07-05 |
+------------+
7-8, 7-9, 7-10, 7-11, 7-12 regardless of the day of the week.
Just learned about the WEEK() function in mysql
SELECT `date` FROM horoscopes WHERE WEEK(`date`) = WEEK(NOW(), -1);
I have a mysql query but it's not working properly yet. The idea is: I have a month calendar, so I select all items with the current month form my db.
I select TV show episodes, they air on a specific date but they are available the next day. I want to have the available dates in my cal. So a specific example: An episode aires 2013-03-31 and is available on 2013-04-01. This episode will not be selected from the db. I have tried something like this:
$this->db->where('MONTH(FROM_UNIXTIME(first_aired+86400))='.$month);
But it didn't work. Does anyone know a solution? This is my full code:
$this->db->select('*, episodes.overview as overview, episodes.id as ep_id, shows.id as id');
$this->db->from('episodes');
$this->db->join('shows', 'episodes.imdb_id = shows.imdb_id');
$this->db->where('CHAR_LENGTH(episodes.ep_title) > 1');
$this->db->where('MONTH(FROM_UNIXTIME(first_aired))='.$month);
$this->db->where('YEAR(FROM_UNIXTIME(first_aired))='.$year);
$this->db->order_by('episodes.first_aired', 'DESC');
$data = $this->db->get()->result_array();
Thanks in advance!
Use MySQLs DATE_ADD function:
select mydate, date_add(mydate, interval 1 day) from testdata;
So if you want to get the month of the next day, use month(date_add(...))
mysql> select mydate, date_add(mydate, interval 1 day) "next day", month(date_add(mydate, interval 1 day)) "month of next day" from testdata;
+------------+------------+-------------------+
| mydate | next day | month of next day |
+------------+------------+-------------------+
| 2013-01-31 | 2013-02-01 | 2 |
| 2013-01-01 | 2013-01-02 | 1 |
+------------+------------+-------------------+
2 rows in set (0.00 sec)
So for your query, use something like $this->db->where('MONTH(DATE_ADD(first_aired, INTERVAL 1 DAY))='.$month);. Oh, and you might have to add a FROM_UNIXTIME here and there.