php mysql search birthday between two dates - php

I need to find the birth day of people from the table .. coming in next 7 days from today.
I have a query ..SELECT * FROMtableWHEREdobLIKE BETWEEN %-08-17 AND %-08-24 but it returns the records whose dates are not submitted in database..i mean the entry is 0000-00-00
I have stored the birthdates in dates format in table. Please Help me finding the bug.

Since this is mysql, I don't know if DATE_FORMAT() can work on this. But give this a try.
SELECT * FROM users WHERE DATE_FORMAT(dob, '%c-%d')
BETWEEN DATE_FORMAT('1983-08-17', '%c-%d')
AND DATE_FORMAT('1983-08-24', '%c-%d') OR (MONTH('1983-08-17') > MONTH('1983-08-24')
AND (MONTH(dob) >= MONTH('1983-08-17')
OR MONTH(dob) <= MONTH('1983-08-24')))
any year can be used (just to complete the date format) since year does not matter
UPDATE 1
Tested it on SQLFiddle.com
SQLFiddle Demo
UPDATE 2
I'm sorry for my first answer. I honestly missed to read this line coming in next 7 days from today. And I think that was the reason why I was downvoted by Imre L. He has his point. The reason why I posted the answer like that was because I thought the OP was asking for the days in between regardless of the year. So here is the update.
SELECT ....
FROM ....
WHERE DATE(dob) BETWEEN NOW() AND NOW() + INTERVAL 7 DAY
Hope it's clear now. :D

this will handle correctly cases wen there is a month or year change between the date range:
select *
from people
where (DAYOFYEAR(dob)+IF(DAYOFYEAR(CURDATE())>DAYOFYEAR(dob),1000,0))
between DAYOFYEAR(CURDATE())
and (DAYOFYEAR(CURDATE() + INTERVAL 7 DAY)+IF(DAYOFYEAR(CURDATE())>DAYOFYEAR(CURDATE() + INTERVAL 7 DAY),1000,0))

By converting the dob date into this year's date you can avoid issues where the period crosses a month or year boundary. This selects all rows where the birthdate occurs in the coming week:
SELECT * FROM users
WHERE concat( year(now()), mid(dob,5,6) )
BETWEEN now() AND date_add(now(), interval 7 day)

SELECT
str_to_date(DATE_ADD(dob, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(dob)) YEAR), '%Y-%m-%d') BIRTHDAY,A.*
FROM app_membership A
WHERE str_to_date(DATE_ADD(dob, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(dob)) YEAR), '%Y-%m-%d')
BETWEEN str_to_date('15-10-2017','%d-%m-%Y') and str_to_date('10-11-2017','%d-%m-%Y')
ORDER BY BIRTHDAY ASC;
Try this. Worked for me.

Lets list people who born in any month/year between december 14 and august 24. We know its an year before another one. We must count with months in the previous year. It's complex because you may have a problem comparing the month of the starting date with the month of the ending date. However, it may be solved with this query:
SELECT * FROM t_users WHERE (DATE_FORMAT(d_birth, '%m-%d')
BETWEEN DATE_FORMAT('2017-12-14', '%m-%d') AND
DATE_FORMAT('2018-08-24', '%m-%d'))
OR(MONTH('2017-12-31') >= MONTH('2018-08-24')
AND (MONTH(d_birth) >= MONTH('2017-12-31')
OR MONTH(d_birth) <= MONTH('2018-08-24')))

Related

How to make this time window query?

I have a MySQL table with the following columns:
id, month, day, remind_days_before
It's for a simple recurring reminder function. The user can enter for example: "I have a very important thing on the 5. April every year, please remind be 15 days before that". There could be tens of thousands of entries in this table. Using this table I want to run a cron_job every morning, which is sending out this reminders in form of email messages. The problem is I don't know how to write this kind of SQL query... Is it even possible? I want to query only those rows where the "month" and "day" as DATE is between TODAY and TODAY+31 days (this is the maximum number for remind_days_before).
I'm trying with this right now, but the $end_month is giving me the same as the $today_month:
$today_month = date('m');
$end_month = date('m', strtotime('+31 days', $today_month));
What happens when the +31 days DATE is in the next YEAR or we have leap year?!
Can someone help me out here?
Thank you very much.
First of all, your fields must be a no-mysql function.
So replace month/day with planning_month/planning_day, because the month and day words, are the functions of MySQL, and you may have errors if they are not quoted
Here's a working query. I've tested and works great:
select * from MyTable
/*CHECK CURRENT YEAR*/
where date(concat(year(now()), planning_month, planning_day)) =
date(date_add(now(), interval remind_days_before DAY))
OR
/*CHECK NEXT YEAR*/
date(concat(year(date_add(now(), INTERVAL 1 YEAR)), planning_month, planning_day)) =
date(date_add(now(), interval remind_days_before DAY))
Live Example
Your, it is a very useful question for me too
Just check for both years:
select *
from your_table
cross join (select year(now()) as current_year, year(now())+1 as next_year) vars
where curdate() + interval remind_days_before day = concat(current_year,'-',month,'-',day)
or curdate() + interval remind_days_before day = concat( next_year,'-',month,'-',day)

MySQL Query - Records between 15 days ago End_date to till End_date

I want to return records from database 15 days old from end _date to till end_date!
I am searching for the query for last 3 days!
However. I want your help to do a query. Its simple but I'm not sure how to do it.
I wrote query something like :
SELECT *
FROM bid_post
WHERE ending_date
BETWEEN DATE_SUB( DATE(`ending_date`) , INTERVAL 15
DAY )
AND ending_date >= CURDATE()
But it is not working !
The data column is a Varchar type. I am storing date as YYYY-mm-dd format
Does somebody now how can I accomplish this?
Thanks.
Please try with this query
SELECT *
FROM bid_post
WHERE ending_date between DATE_SUB( CURDATE() , INTERVAL 15 DAY )
AND CURDATE()
You should never store dates as varchar since these are not real dates and you need to store them using mysql native date data types.
Since the format is Y-m-d you may not need to do a conversion using str_to_date function and can use the query as
select * from bid_post
where
ending_date between date_sub(curdate(),interval 5 day) and curdate()
This will give you data from last 15 days till today.
Using conversion to real date you need str_to_date as
select * from bid_post
where
str_to_date(ending_date,'%Y-%m-%d') between date_sub(curdate(),interval 5 day) and curdate() ;
DEMO

Display only future database records with MySQL

I am trying to display only records two weeks in advance from the current date onwards. starttime is stored as a datetime data type in the database.
I have this,
SELECT id, date_format(starttime, '%d-%m-%Y %H:%i') AS formatted_start, date_format(starttime, '%Y-%m-%d') AS formatted_date,
date_format(endtime, ' %H:%i') AS formatted_end
FROM timedates WHERE user_id = 1 AND `status`='' AND YEARWEEK(formatted_date, 0) IN (YEARWEEK(NOW(), 0),
YEARWEEK(DATE_ADD(NOW(), INTERVAL 2 WEEK), 0))
But I am getting a syntax error YEARWEEK(formatted_date, 0) IN (YEARWEEK(NOW(), 0) AND YEARWEEK(DATE_ADD(NOW()
Could anyone tell me what's wrong with it?
It seems MySQL does not support calculated column in the where clause:
try
SELECT id, date_format(starttime, '%d-%m-%Y %H:%i') AS formatted_start,
date_format(starttime, '%Y-%m-%d') AS formatted_date,
date_format(endtime, ' %H:%i') AS formatted_end
FROM timedates
WHERE user_id = 1 AND `status`='' AND
YEARWEEK(starttime, 0) IN (
YEARWEEK(DATE_ADD(NOW(), INTERVAL 1 WEEK), 0),
YEARWEEK(DATE_ADD(NOW(), INTERVAL 2 WEEK), 0))
use starttime instead of formatted_date
As I said, I see no reason to use YEARWEEK function. You need start date set to today which is CURDATE() and plus 2 weeks period which is DATE_ADD(CURDATE(), INTERVAL 2 WEEK) and then we just check if starttime between those 2 dates.
SELECT id, date_format(starttime, '%d-%m-%Y %H:%i') AS formatted_start,
date_format(starttime, '%Y-%m-%d') AS formatted_date,
date_format(endtime, ' %H:%i') AS formatted_end
FROM timedates
WHERE user_id = 1
AND `status`=''
AND DATE(starttime) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 2 WEEK)
How do you define "two weeks in advance"? Our normal inclination, is to think "within two weeks", which would be to simply before 14 days from now.
WHERE starttime >= NOW()
AND starttime < NOW() + INTERVAL 14 DAY
But the original query is using YEARWEEK function, and the behavior with that is a bit different.
If today is Thursday, May 21, 1015. What is the range of dates that starttime should fall into?
For datetimes right now or in the future, we can just do:
WHERE starttime >= NOW()
(We can do just a > rather than >= if we want to exclude startime values that are an exact match for NOW().
The question is, the bit about "two weeks in advance". Adding 14 days, or 2 weeks, would get us up to Thursday, June 4, 2015. It looks like you might also want to include Friday and Saturday, June 5th and 6th. (Up until Sunday, June th.)
We can use an expression to return "Sunday on or following 14 days from today"
If it's a Sunday, we just use that date. (For convenience, we think of that as adding 0 days). If it's Saturday, we need to add 1 day. If it's a Friday, add 2 days, ... Monday, add 6 days.
Conveniently, the expression 6 - WEEKDAY(NOW()) gives us the number of days we need to add to today's date to get to the next Sunday.
Reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekday
We probably want to lop the time off at midnight, we can use the DATE() function to do that.
-- startime values on or after now and before midnight
-- of the first sunday on or after the date two weeks from now
WHERE starttime >= NOW()
AND starttime < DATE(NOW()) + INTERVAL 14+6-WEEKDAY(NOW()) DAY
It's much easier to read and understand what the query is doing when we reference bare columns from the table on one side, and do the gyration and manipulation on the other side. It's also easier to test. And, maybe more importantly, it allows MySQL to make effective use of an "range scan" operation on a suitable index, rather than having to evaluate a function on every row in the table.
If today is '2015-05-21', and what you meant by "two weeks in advance" was up until the second Sunday from now (Sunday, May 31, 2015... 1 week and 3 days from now) just replace the 14 with 7.
Again, it really depends on how you define "two weeks in advance", how you determine what that ending date boundary is. Once you can define that, you can write an expression that returns the value, and just compare to value stored in the starttime column.

MySQL Date and Interval - records between dates

I've got a syntax problem I can't sort out. I'm just trying to grab all records from last 3 days.
$result = mysqli_query($link,"SELECT * FROM records WHERE today BETWEEN CURRENT_DATE AND DATE_ADD(CURRENT_DATE, INTERVAL 3 DAY)");
today is DB column for the MySQL timestamp and looks like this: 2014-10-30 16:35:58
This query only gives results for 1 day, not 3. Can someone help with the syntax problem?
DATE_ADD(CURRENT_DATE, INTERVAL 3 DAY) means three days in the future, not three days ago. Unless the today column is supposed to represent (say) the date for which a future appointment is scheduled, you usually want to subtract days from a date. So get three days ago, you need to use DATE_SUB. I'd recommend this query:
SELECT *
FROM records
WHERE today >= DATE_SUB(CURRENT_DATE, INTERVAL 3 DAY)

Getting an event from a database a week in advancee

I am currently developing a sports website where one of the pages with be forthcoming fixtures in which the user will be able to what team and where the team are playing their next match.
I have a database with the following fields...
ID
TEAM NUMBER
OPPOSITION
VENUE
DATE
MEET TIME
MATCH TYPE
So a row of data pulled from the DB and print_r'd may look like this
ID=>[1] TEAM NUMBER=>[1] OPPOSITION=>[YORKSHIRE] VENUE=>[HOME] DATE=>[2009/4/25] MEET TIME=>[13.00] MATCH TYPE=>[CUP]
My problem is i cannot work out how to show the next match dependent on what the current date is, so for example for now I want the site to show all the games that will happen over the weeken of the 25th April 2009 and then once that has gone the fixtures for the next weekend.
Hope this makes sense and some one give me an idea of how to tackle this.
select * from my_events where date between now() and date_add(now(), interval 7 day);
Should do it I think.
Instead of relying entirely on MySQL, you can also use PHP's strtotime() function:
$query = "select * from my_events where date between now() and ".
date("Y-m-d", strtotime("+1 week"));
For MySQL check out the Date and Time functions. You can use a combination of CURDATE() and ADDDATE() to achieve what you need.
Your description is very vage but try something like this:
SELECT all_fields_you_need
FROM table_name
WHERE `DATE` > CURDATE() AND `DATE` <= DATE_ADD(CURDATE(), INTERVAL 7 DAY)
ORDER BY `DATE` ASC
(not tested, just written as it came into my mind...)
Load it all into an array and display the data
you can get the system date (in Oracle using sysdate) and then add to it, so look for all records where DATE = sysdate + 7. You may have to play with this a little, formatting the date so that sysdate + 7 returns a date without the time, but that is basically what you need.
EDIT:
If you want the event between now and a week from now (if games are only on the weekend, then this will return next weekend's games) do
DATE > sysdate AND DATE <= sysdate + 7
To get the next match for team xxx
SELECT *
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE = ( SELECT MIN(DATE)
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE > NOW() )
I suspect this is what you really want, if matches only take place at weekends (which seems to be an assumption from your question).
Today + 7 days is not the same as next weekend unless today happens to be the same day of the week as the match.

Categories