I'm working on a invoice system for a application of mine. I want to invoice the users every 2 weeks. There is a cronjob every week with the check if the user gets an invoice. But it give me some bugs, because it has been a new year and the system gives someone a invoice even when they have got them a week ago.
This is my query:
SELECT *
FROM user
WHERE DAY(registered) = DAY(DATE_SUB(CURDATE(), INTERVAL 2 WEEK))
OR DAY(registered) = DAY(NOW()) AND registered != CURRENT_DATE()
Thank you!
You can try this- (code is tested) Demo
SELECT * FROM user WHERE registered > CURRENT_DATE() AND
MOD((FLOOR( DATEDIFF( now( ) , `registered` ))),14) = 0
The day function in MySQL returns the day of month. You will clearly have problems with your query when the time period spans month boundaries.
I think you want logic of this form:
SELECT *
FROM user
WHERE registered >= DATE_SUB(CURDATE(), INTERVAL 2 WEEK)) and
registered < curdate()
(Or <= if you want the current date to be invoiced.)
I do not know what this is doing:
OR DAY(registered) = DAY(NOW()) AND registered != CURRENT_DATE()
If the day of the month is the same for registered and now(), then registered should be the current date. The only difference between now() and curdate() is that the former includes the time stamp. I would also say that it is bad practice to mix three different ways of getting the same date in the same query, even if they are equivalent.
Related
I am basically trying to get my app to report when items are not returned or overdue in a 5 day and 15 day window. After much trial and error the below coded is the best I could come up with. However, when I extend to 15 days I still receive items that were returned (hence not exists not working appropriately) based off of movieid. The goal is see items from checkout that have not been returned with a time frames to notify members of late dates. Any suggestions to improve this statement?
select movieid, dueback
from checkout as a
where NOT exists
(
select * from returns as b
where a.movieid = b.movieid
AND dueback < DATE_SUB(NOW(), INTERVAL 5 DAY)
)
ORDER by dueback;
CHECKOUT TABLE: checkoutid, outdate, dueback, movieid, customerid, payment
RETURNS TABLE: returnid, today, movieid
You can use BETWEEN using the two computed dates. See Between documentation, but you can do something like:
AND dueback BETWEEN DATE_SUB(NOW(), INTERVAL 5 DAY) AND DATE_SUB(NOW(), INTERVAL 15 DAY)
That way you will get all dueback that is between 5 and 15 days from it's date.
I am working in an application where we storing doctor appointments schedule weekday basis. For this purpose, we maintaining a schedule table and a calendar table that stored the date.
Here is the table snapshot of the schedule table
In our application, when a user selects a date the doctor's appointment list is showing based on that date dayname. Now I am getting date range wise required data of a doctor like the following query where no timezone used.
select datefield as date,
(select count(id)
from appointment_schedules
where substring(dayname(calendar.datefield),1,3) = appointment_schedules.week_day
and user_id=$user_id
)
from calendar
where datefield between '$start_date' and '$end_date'"
Now we implementing timezone wise appointment list where the appointment list will be shown based on user timezone. Since the weekday is fixed in the schedule table, I don't find out the correct way how to retrieve considering timezone. For example, if the user timezone difference is 8 hours from the server then in a certain period user day will ahead or behind from the server. I am stuck on how can I show user timezone day-wise accurate data in a single query.
Any suggestions will be highly appreciated.
Thanks in advance
Some rough code below for how you can handle it, I haven't tested this but it should give you enough to fix your queries.
PHP:
$utc_difference = -12;
$week_day = "SUN";
$prev_week_day = "SAT";
$next_week_day = "MON";
MySQL:
WHERE
(DATE_SUB(start_time, INTERVAL $utc_difference HOUR) < 0 AND week_day = $prev_week_day)
OR
(DATE_SUB(start_time, INTERVAL $utc_difference HOUR) > 24 AND week_day = $next_week_day)
OR
(DATE_SUB(start_time, INTERVAL $utc_difference HOUR) > 0 AND DATE_SUB(start_time, INTERVAL $utc_difference HOUR) < 24 AND week_day = $week_day)
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)
I am trying to write a query to select all records from users table where User_DateCreated (datetime field) is >= 3 months from today.
Any ideas?
SELECT *
FROM users
WHERE user_datecreated >= NOW() - INTERVAL 3 MONTH
If you want to ignore the time of day when a user was created you can use the following. So this will show someone created at 8:00am if you run Quassnoi's example query at 2:00pm.
SELECT *
FROM users
WHERE DATE(user_datecreated) >= DATE(NOW() - INTERVAL 3 MONTH)
Using DATE(user_datecreated) prevents mysql from using any indexes on the column, making the query really slow when the table grows.
You don't have to ignore the time when the user was created if you remove the time from the "3 months ago" date, as all the users created that day will match the condition.
SELECT *
FROM users
WHERE user_datecreated >= DATE(NOW() - INTERVAL 3 MONTH);
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.