check if dates are between date range in sql (check without year) - php

I am developing a holiday guide site!
I am using a MySQL database in which I store the information about hotels.
I have 4 date fields in order to store the 2 possible seasons that works each hotel (date_from, date_to, date_from_extra, date_to_extra). I store the dates in a format like this: DD/MM, for example: 25/12.
I am trying to check if the date1 of first datepicker of the site and the date2 of the second datepicker are between first range (date_from, date_to) or between second range (date_from_extra, date_to_extra) in the database.
The problem is that I don't use years in database date fields and is difficult to compare dates especially when the first or second season in database doesn't finish at the end of the year but it lasts more, for example (14/11 - 25/02).
Any help?
Thanks a lot!

try this
$todayDate = date("Y-m-d");
$check_date= strtotime($todayDate);
use this varible to check
SELECT table_fiels FROM table_namE WHERE month(check_date) BETWEEN month($your_db_start_date) AND month($your_db_end_date) AND day(check_date) BETWEEN day($your_db_start_date) AND day($your_db_end_date)

Related

How to compare date inserted from textbox with date from MYSQL db

I want to compare date inserted in textbox with date from MYSQL database.
My idea is to make car rent form (with FROM date - TO DATE), and after submiting that form, query will check if it is possible to rent that specific car in that period. Anyone had anything similar?
Thanks
We run many queries that are Start and End Date dependent.
SELECT * FROM rentCar
WHERE textBox BETWEEN StartDate AND EndDate

How to select data by using range of dates in MySql query?

I have a problem related to MySQL query, I use WAMPServer.
I have data in database which have range of dates but when I select data for example
select * from CHD WHERE addtime>='2018-06-15' and addtime<='2018-06-21';
It displays data from '2018-06-15' to '2018-06-20', data of 2018-06-21 are not displayed even if I do
select * from CHD where addtime='2018-06-21';
is not working
Please anyone can help me
This assumes that your column is of type datetime.
The shorthand version of your date in the filter clause is assumed to be at midnight of the date. Your values that you are attempting to retrieve have times after midnight of that date. You either need to define a timestamp along with the date, or you need to filter by the day after for less than equal to or the day before for greater than equal

error in save date in database with php

want to be able to store the current date from PHP in to a mysql table.
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."','".$date."','".$yes."')";
Date's column type in database is DATE.
but date save in database like this: 0000-00-00
help me.
try this
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."',".$date.",'".$yes."')";
or
if you want to inset today's date use
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
I need to store Date field in database for example VARCHAR(10) becouse, DATE type in mysql server don't support persian (farsi) date.
If you are getting the current date, just use one of the built-in MySQL functions for that:
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
You won't ever have to worry about formatting or any sort of funny inputs and you can still do all sorts of addition, subtraction and the like using one of the many other functions.
Edit: I have just looked up some information on the date system and you will run into problems trying to squeeze that data into a date field. The first six months have 31 days, the next 5 have 30 days, then the last has 29 - unless it is a leap year in which case it has 30. The date field simply won't LET you insert these values into a date field.
You might need to convert your data to a timestamp and write a function to encode/decode it into the format you need.

Pull three upcoming events from MySQL database

I have a database of events that are used to fill a PHP calender. I want to also have a page that lists three upcoming events in date order when ever the page loads. The records have a separate field for day, month and year. The issue i run in to is getting records that accrue after the current date.
Table structure will be good to have but I will guess the query that you need. Use that as a point to start from.
SELECT * FROM events WHERE CONCAT(year,'-',month,'-',date) > DATE_FORMAT(NOW(),'%Y-%m-%d')
ORDER BY year DESC, month DESC, date DESC LIMIT 3;
You are going to have to build a date from your three fields to test against current date, or split current date into year, month, day and test all three against data in your table.
Got to say, not a brill decision on someones part to split date like that.
Need table structure before we can properly help query wise.

How to sort data depending on what day it was posted?

How do you sort data which was stored in a mysql database depending on the days of the week in which the data was submited ??
I basically want to create a diary which outputs information in each day of the week depending on what day it was posted by dates so,
Mon - Data in order of date
Tue -
Wed - e.t.c
Any code examples and information will be great, thanks.
You can do a
SELECT DAYOFWEEK(datehere) as dayofweek, datehere FROM something ORDER BY dayofweek, datehere;
You can use the DAYOFWEEK function to extract the day, and then sort on it just like any other data.
What kinf of data type is the column where you store the date submission?
It seems like you're asking for a basic SELECT statement?
SELECT some_column, another_colum FROM your_table ORDER BY your_date_column DESC
This assumes you actually have a column that logs the insertion timestamp.
If this answer is obnoxiously simplistic, please forgive me...and give us more details :)
Regards.
If your data is stored as a DATE or DATETIME field, use the DAYOFWEEK or DATE_FORMAT functions to turn it into day name for output, but continue to order by the DATE field
SELECT DATE_FORMAT(my_date_column, '%W') AS dayofweek
FROM my_table
ORDER BY my_date_column
Well, the sorting bit is easy, just sort on the column that represents the post's date. The grouping in days is something you can do in your code, since you need to check the date there anyway (for post-processing the actual output).
To put it this way, you can do a subselect to get the specific day of the week, but in your code you would have to check the day again to group posts per day. In that case it's better (and cleaner, since you're separating business logic from data) to do this in your code, something like this:
select all posts (within date range)
make an associative array, with the
days as keys, and posts (in new
arrays) as values
loop through the
days and then posts to output the
posts per day
SELECT *
FROM diary_entries
ORDER BY FIELD(DAYOFWEEK(created), '2, 3, 4, 5, 6, 7, 1'), created
DAYOFWEEK grabs day of the week number (1 = Sunday).
FIELD makes Monday first day of the week.
After sorting by day of week, then sorted by date created.

Categories