Check overlapping dates using PHP and MySQL - php

I am planning to develop a cash back module in the following scenario:
There will be test cases that admin can set a cash back for one product whose date range conflicts with previously defined cashbacks on the same product.
For example:
admin set a cash back for a product P1 as (possibilities):
26-Dec-2016 to 29-Dec-2016 10%
25-Dec-2016 to 30-Dec-2016 10%
24-Dec-2016 to 31-Dec-2016 10%
How to handle the date range so that cashback date range does not overlap with other dates?
Or admin can not set a new cash back if a date range falls in previously defined cash back for a specific date range. If we can go with this then how to manage (how to check date range is falls in another one).

I'm assuming, Inside database you are storing the cashback as a row with the CashbackStartDate and CashbackEndDate as columns.
When ever setting a new date range, say newStartDate and newEndDate, first search for all the cashbacks with CashbackStartDate less than newEndDate and then CashbackEndDate grater than newStartDate.
This will grab all the cashback with the overlapping dates, for you.
Back to your question, just check if any rows are returned, if yes then .....you know what to do.
Sample SQL code:
... WHERE newStartDate < CashbackEndDate
AND newEndDate > CashbackStartDate;
This should get you all the overlapping dates with partial and total overlap.

Related

How to calculate total online time using laravel?

I want to calculate particular user's total online time for selected date. Below is MySQL table -
User Active Status Table
In status column I store values as 0 (Offline) and 1 (Online).
As you see user_id 33 is online at -
10:05:25
14:00:00
and offline at -
11:45:58
19:00:04
So, How to calculate total online time using Laravel?
Thanks in advance!
You can try to refactor a bit your database..
Add 3 columns date, logged_in_time, logged_out_time and you can make an event listener each time user logs in our out. Any time they will log in our out it will be stored in your table and you can filter easily by dates in your query.
If you want to calculate the amount of time they were logged in for a specific date you will just do 'logged_out_time - logged_in_time'

Calendar with time slot reservation

I'm very new in this site. Spend a lot of time for searching an answer for my target, but unsuccessfully. So, there it is (sorry for my english):
I'm quite new in php, but already created nice stuff. But now I'm interested in calendar with time slot reservation. I saw good exemple then I registered for my doctor. There i was able to choose a day from calendar and then select a free time range (e.g. from 12:00 to 12:30).
First idea for this is some calendar in PHP and table in MySQL. In MySQL table few rows with id, name and quantity. id - unique number of row, name - date and time slot, and quantity - (1 = free cell, 0 = unable). In PHP calendar after selecting a free time slot, in DB quantity goes from 1 to 0 and apear unavailable for other users.
But if where are a lots of "doctors"? How to make calendar, DB for thousands of them?
For the beggining maybie someone have some example how to make calendar with time reservation (e.g. 1 hour) in the easiest way? Or suggest something?

insert select statement with changes to fields and logic thinking

I'm working on some php code having to do with creating recurring invoices. I need some help thinking through the logic. I think ultimately I will end up with a type of Insert/Select statement. The php code that is ultimately generated here will run in a nightly cron.
First, let me show you some of my table items for a frame of reference and explain a bit about what they do. The table is called Invoice and there are other tables associated with it, but I think if I can nail now this one table's logic, I can apply it to the others.
ID = This is the primary key that auto increments and also happens to be the invoice number.
recurring = This is whether the invoice is one time or recurring. O=One time, M=Monthly etc
recurred = This will be set to N on insert and is the logic I'm using to see if the next recurring invoice that will be created in the table should be based off this invoice or not. When a new recurring invoice is inserted into the table, this will be changed to Y so its not processed again by the cron job.
date = This is the date that the invoice was inserted/created. For example, if date the invoice was created is the 15th of this month, the next invoice created will be the 15th of next month. Essentially M represents monthly. However, I haven't figured out yet what to do about a created date of the 31st but the next month only has 30 days, could use some help here.
So here is my logic. First, I run a query to find all rows that are not O and recurred = N.
$result = mysql_query("SELECT * FROM invoice WHERE recurring != 'O' and recurred
= 'N'") or die(mysql_error());
Next, I work on each kind of recurring M for monthly, Q for quarterly, etc..
if recurring = M
if todays date day = 'date' day
copy line item into a new row with the following changes: new ID, todays date, and mark the recurred to N for the new insert (will mark the old one Y)
Obviously that is not actual code, just what I'm thinking about.
So my questions are 1.) How to create a insert select statement to deal with changed variables and 2.) how to deal with dates where if the invoice was created on the 31st of one month and the next month only has 30 days (or 28).
take only rows that need to be re-inserted:
SELECT *, DATEDIFF(`date`, NOW()) as diff FROM invoice WHERE recuring != 'O'
AND recured = 'N' AND diff >= 30;
iterate through that list and use a new date for each row by calculating it with:
$newDate = date("Y-m-d", strtotime($oldDate+" +1 month"));
This should solve your 30th day of month problem.
Now insert newly created data.
Also, good advice to use enum for recuring field, and boolean for recured.
I have made somewhat similar application. In my case I had 2 tables: actual invoices and recurring invoices. Recurring table had one field called NextDate - the date when next invoice should be created. One time invoice is added directly into invoices table, otherwise the cron job scans the recurring table and checks for a NextDate value. If it is in past then new invoice is generated into invoices table and NextDate is updated (normally incremented by month).
In my case incrementing by month changes month value only by 1. For example in MySQL
DATE_ADD('2008-01-31', INTERVAL 1 MONTH) --> 2008-02-29

Need to store multiple date range values and retrieve them via sql

I'm building a home rental system. The system stores profiles of homes and users can book homes for rent for periods as between one day to a year. I've got the booking part all set up except I am faced with a requirement from teh client to be able to set certain dates and date ranges as un bookable for homes.
A home can be available for a whole year for rent, or can be available for 6 months, or be unavailable on discreet days eg: Holidays and weekends for summer homes etc.
I'm perplexed as how would I be able to set up a database table to store this information considering that the information must be retrievable by a sql query. I mean consider the following situation, a home can be rented through out the year except on wednesdays, the 4th of July, 10 November to 25th December and 31st December.
How do I store this in a database and be able to run a query to check for a homes availability between set dates. I'm wokring in php MySql
There are two different concepts that you're describing: the "on Wednesdays" is not as much of a date range as it is a recurrence pattern. The same goes for "weekends".
You're probably looking at two different tables in addition to your property table that define these unavailable dates: one that represents specific date ranges and one that represents recurrence patterns.
Property
|
-------------- ---------------
| |
PropertyUnavailableRecurrence PropertyUnavailableRange
(Bear in mind that you might want to figure out shorter names)
PropertyUnavailableRecurrence would need to store the information necessary for turning "Wednesdays" and "weekends" into viable decision logic. I can't model this for you, since all of you've presented in this pattern are specific days of the week, but I'd imagine that you'd need to be able to account for "First of the month" or "Second Wednesday of the month", but I don't know. In any case, this is where you'd need to store that information.
PropertyUnavailableRange would just contain simple From and To dates that define the range. This part is pretty simple.
Of course, an alternative would be to take the recurrence patterns specified in the application and turn them into discreet PropertyUnavailableRange records, but you'd still need to set up a table to store these recurrences and associate the discreet records with a recurrence so that you could manage them.
One approach is to have a table, PropertyUnavailable, with the following structure:
create table PropertyUnavailable
(
property_id number not null,
when date not null
);
This table would have a row for each day that the property is not available because of a black out period (e.g., every Wednesday, Holiday, etc). I am ignoring how you will store the meta information of the pattern -- all this table wants are rows for each day where the property is not available because of a blackout period.
I assume you will also have a table for reservation days, PropertyReserved, with the same structure as above plus a foreign key to reservation_id (or something similar).
Now to see what day's are unavailable/reserved for a given date range, the sql would be something like this:
SELECT a.when, 'blackout'
FROM PropertyUnavailable a
WHERE a.when between <from_date> to <to_date>
UNION ALL (
SELECT b.when, 'reserved'
FROM PropertyReserved b
WHERE b.when between <from_date> to <to_date>
);
If nothing is returned with the query, then the property is available between the date range specified (from_date, to_date).
Did you consider simply booking those "unbookable" dates in the name of the system?
It appears that you are not clear about what is required to be stored in teh database; and what is required in code segments. Set the "unbookable" dates aside for a moment, assume you only have actual booked dates. Catcall has a point. What does your current code look like, when you search for available dates ?
SQL is quite capable of handling dates and performing date arithmetic. My NonSQL is not, you will have to store more in the databse than in real SQL. But you do not need to store rows per date. The Reservation table needs FromDate and ToDate only.

Counting records for particular ranges dates and times

I have a database that stores details of money-off vouchers generated on our online system, there are four specific voucher types, and each time a voucher is generated, it stores a record in the database with a DATETIME and the voucher type.
I have to create a very specific spreadsheet report for a client that accepts two dates as ranges (so they say "generate a report between these two dates"), and then goes through EVERY DAY between those two ranges, and reports how many vouchers of that type were generated per to the closest hour. So a sample left hand column would look like this:
27/7/2010 07:00
27/7/2010 08:00
27/7/2010 09:00
27/7/2010 10:00
if there are no vouchers generated during a certain hour, it isn't shown on the spreadsheet.
Basically, i need some help figuring out how to generate this! i can generate a .CSV file, that's the simple part in my opinion. Apart from that, i don't have a clue where to begin.
SELECT DATE_FORMAT(YourDateColumn, '%d/%M/%Y %H'), Count(Vouchers)
FROM Table
WHERE YourDateColumn between StartDate and EndDate
GROUP BY DATE_FORMAT(YourDateColumn, '%d/%M/%Y %H')
ORDER By 1

Categories