Selecting campground availability by date - php

I am working on a campsite management system and have been trying to figure out date selection query.
$sdate is the starting date of the reservation
$edate is the end date of the reservation
I am simply trying to find the sites that are not currently reserved between the dates.
Right now it works fairly well, except in the case where someone wants to reserve the same day that someone is leaving, it is considered taken because someone is "in" that spot, but will be leaving that day so it shouldn't show up.
Date format is mm/dd/yyyy
Format in the database is not set to date, is set to varchar with the same format.
$searchSite= $wpdb->get_results("SELECT *
FROM wp_campground_sites
WHERE wp_campground_sites.id
NOT IN
(
SELECT wp_campground_sites.id
FROM wp_campground_sites
JOIN wp_campground_reservations
ON wp_campground_sites.id = wp_campground_reservations.site_id
WHERE
(
'$edate' between reservations.sdate and reservations.edate
OR
'$sdate' between reservations.sdate and reservations.edate
OR
reservations.sdate between '$sdate' and '$edate'
)
");

you have to change your varchar string to date format and compare like
select date_format(str_to_date('12/31/2011', '%m/%d/%Y'), '%Y%m');
because you are going to compare string in with date result will not display until you use date_formate function to convert varchar datatype to actual date format.

Related

MySQL query: Date > Date not working

Hi there please help me if you can. Here is my senario:
I have a MySQL database with a column that holds a date in the form of a varchar. The format of the date is the following 29/05/2014 (i.e. d/m/Y).
I'm trying to compare the value of this column with todays date and return any rows where the date is earlier than todays date.
I'm using a php variable to store todays as follows:
$date = date("d/m/Y");
Here is my SQL query:
SELECT * FROM patients WHERE last_seen < '$date'
What gets returned
So what is returned is very unusual (to me). All records where the last_seen "day" is less than todays "day". It seems to be overlooking the month and year. So in other words if I last_seen = "30/05/2014" and todays date is "29/05/2014" this record is still returned.
Does anyone have any ideas what I might be doing wrong here?
Thanks
You really, really shouldn't store dates in a varchar field - use date or datetime or timestamp data type.
That said, sometimes you don't have control over the database and you have to deal with somebody else's bad design decision. In this case, to compare dates, convert the varchar strings to dates and compare them that way. So, in your case, you can have something like this:
$date = date("d/m/Y");
and then
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < str_to_date('$date', '%d/%m/%Y')
or simpler
SELECT * FROM patients WHERE date(last_seen) < current_date
This way you are actually comparing dates and not strings containing dates. Naturally, this assumes that all dates are stored in the same format.
EDIT: I just tested the last option - and, apparently, date('30/05/2014') returns NULL on my system (mysql 5.5 on linux), hence I suggest the best way is
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < current_date
You need to store your date as DATE or DATETIME in your database.
Then you can use:
SELECT * FROM patients WHERE DATE(last_seen) < CURRENT_DATE

compare user entered date with the date field stored in database

I am creating a website where user can create entertainment programs on a particular venue.I need to check whether there is any program fixed on that particular venue for the start time and end time entered by the user.
I have 2 column names in database named starttime and 'endtime' which contains the value in the form of yyyy-mm-dd hh:mm:ss. I think I need to convert the value stored in the both fields in database to timestamp. But how to write the query.
eg:
SELECT *
FROM table_name
WHERE
timestamp(starttime)>='user entered value'
AND timestamp(endtime)<='user entered value '
Is this query possible.?
If "user entered value" is unix timestamp, you can use UNIX_TIMESTAMP(field) function to convert that field to timestamp. If it's not, you have to first convert it to timestamp using strtotime or similar function. Or you can go the other way, and use datetime value inside the query by converting string to datetime using STR_TO_DATE(date, format) MySQL function.
For instance, if user enters the date in YYYY-mm-dd hh:mm format, use following query:
SELECT * FROM table_name WHERE starttime >= STR_TO_DATE('2012-02-01 12:00', '%Y-%m-%d %H:%i') AND endtime <= STR_TO_DATE('2013-01-01 00:00', '%Y-%m-%d %H:%i')
Personally I like to use UNIX timestamps inside the query, but that's just my quirk.
Why not use unix time? It makes dealing with dates much simpler.
$current_time=time();
This creates an integer.
Your query:
SELECT * FROM table
WHERE starttime
BETWEEN starttime AND endtime

Search mysql database for date range

I have a database where users enter a date, among other things, with a drop down calendar, the date format is in this format 21-NOV-2012 .
Is there a way to search the database with a date range ?
I currently search date using the following ::
sql ="SELECT * FROM ircb WHERE date LIKE '%$term1%' AND date LIKE '%$term2%' LIMIT $start_from, 15";
term1 is month and term 2 is year , this does not allow for date range within the month only for the month.
any help appreciated, thanks
I know this doesn't really solve your problem but I guess it's a testing (and you have access to the database) and will risk myself of getting flamed by telling you a better approach, as I will feel guilty if don't tell you what would be the correct way to do it:
You should have a Date type field in your database (I assume mysql) and store the whole date in that field, and mysql can search using that date (normally the standard date format would be YYYY-MM-DD) .
You just need to do:
SELECT * FROM TABLE WHERE date > 'your date';
or ........................... < 'your date';
or .................WHERE date BETWEEN 'date1' AND 'date2';
It will allow you to do those kind of operations in a much easier and human-readable way.
Also have a look at the datetime if you are interested, as you can do the same but with the time of that day also included in the same field! :D
Have a good look at the field types, as it's essential to have the database healthy and optimized.
date >= '$term2-$term1-01' AND date < DATE_ADD( '$term2-$term1-01', INTERVAL 1 MONTH)
I think this should work.
sql ="SELECT * FROM ircb WHERE date BETWEEN '$term1' AND '$term2' LIMIT $start_from, 15";
BETWEEN will let you fetch the range date, and you can delete those LIKE.

Getting values in between ordinary date and timestamp from mysql database

I have a standard mysql timestamp in this format 2011-11-14 20:06:24 . This timestamp will be added whenever a new record is added to the table with the name lead.
I have two input fields for the user to enter from date and to date in dd/mm/yyyy format. Once the user enters both dates and press a button the values will be passsed to other field to get the records from the table lead which are inserted between the time range.
I tried the below query but its not working
SELECT DATE_FORMAT(added_on, '%d/%m/%Y') as date
FROM lead
WHERE added_on BETWEEN "10/11/2011" AND "14/11/2011"
Use standard format for dates, datetimes and timestamps: '2011-11-14' and not '14/11/2011'.
Use single quotes, not double quotes.
If added_on is a timestamp, you should not use BETWEEN or you'll lose almost all records from the last day because '2011-11-14' will be converted to '2011-11-14 00:00:00'. Use this instead:
WHERE added_on >= '2011-11-10'
AND added_on < '2011-11-15' --- note the "< the next day"
or
WHERE added_on >= '2011-11-10'
AND added_on < ('2011-11-14' + INTERVAL 1 DAY)
You should read carefully the MySQL docs: TIMESTAMP properties page for how timestamps are handled (and auto-inserted, updated) in MySQL and the MySQL docs: Timezone support.
you have to convert the date first in php
you can use "date" function of php to covert the date format according to mysql, as follows:
$converteddate = date('Y-m-d',strtotime($yourposteddate));
This will return date in format, e.g. 2011-08-15 which can be understood by mysql and then use it as normal in mysql.
SELECT DATE_FORMAT(added_on, '%m/%d/%Y') as date
FROM lead
WHERE added_on BETWEEN DATE_FORMAT("20/11/2011",'%m/%d/%Y')
AND DATE_FORMAT("21/11/2011", '%m/%d/%Y');

Getting a Date Range in MySQL form a UNIX Timestamp in PHP

I have a UNIX timestamp in a table like 1321521158 and want to get the details from a table for the particular date, like:
$mydate="11/11/2011";
SELECT notes FROM table WHERE `addeddate`=$mydate
How do I get it?
you can use the mysql date function like:
WHERE date(addedate) = date(mydate)
or you can calculate the starting and ending timestamp for the given day you want to know and then do
WHERE addeddate >= mystarttimestamp AND addeddate <= myendtimestamp
try
WHERE $mydate = FROM_UNIXTIME('dateColumn')'%Y/%D/%M');
MySQL Date Time Functions

Categories