PHP/MySQL Date Search - php

My script works fine for all sales but skips all sales on the 15th.
The MySQL rows for the period look like this:
ID: 10 START: 2010-12-01 END: 2010-12-15
The MySQL rows for the sales look like this:
DATE: 2010-12-15 20:40:26
$period_info=mysql_fetch_array(mysql_query("SELECT start,end FROM period WHERE id='$period' LIMIT 1"));
$start=$period_info["start"];
$end=$period_info["end"];
$total_sales=mysql_num_rows(mysql_query("SELECT * FROM sales WHERE seller='$seller' AND date BETWEEN '$start' AND '$end'"));
And ideas?

This is because MySQL uses 00:00:00 as the time for the DATE type. I think you can use:
"SELECT * FROM sales WHERE seller='$seller' AND (CAST date AS DATE) BETWEEN '$start' AND '$end'".

Only compare the date part of the datetime, and ignore the time part:
WHERE DATE(`date`) BETWEEN '$start' AND '$end'

For best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.

Related

Mysql CURDATE() not work with timestamp type

I've field with datatype as timestamp in mysql table
fetch the rows that matches the current date
SELECT * FROM revenue WHERE date=CURDATE()
But it returns the empty result set
You need to compare the date part of timestamp as timestamp itself represents time in milliseconds, e.g.:
SELECT *
FROM revenue
WHERE DATE(date) = CURDATE();
You should be able to do the following:
SELECT * FROM revenue WHERE DATE(date)=CURDATE()
DATE(date) should give you a date in this format: YYYY-MM-DD as well as CURDATE()
use CURRENT_TIMESTAMP, modify it to your format requirement, with TO_CHAR specifying the format. Your query may look like this depending on the date format.
SELECT *
FROM revenue
WHERE date=TO_CHAR(CURRENT_TIMESTAMP(), 'DD-MON-YYYY');

PHP count mysql row with changed date format

I have next problem:
My table date format was: LIKE 2017-01-08 18:50:25 (with time).
When i use sql query like
'SELECT date FROM table WHERE date = "2017-01-08"'
My row was empty, i need COUNT all row with same (today) date WITHOUT TIME.
Note, i will not change INSERT date time!
Use DATE() to get the date portion of the datetime field and compare it to today. Use COUNT() to get the number of records that match your query.
SELECT count(*) FROM table WHERE DATE(date) = CURDATE()
You can also replace CURDATE() with NOW(), CURRENT_DATE(), and CURRENT_DATE
You can also use it in the following way
'SELECT date FROM table WHERE date_format(date,'%Y-%m-%d') = "2017-01-08"'
the date_format is mysql function which return date according to your pattern the above pattern only return the Y-m-d from the datetime
I hope it will help you
plz change your statement equal operator to greater than
'SELECT date FROM table WHERE date > "2017-01-08"'
as by default if time portion is not present then it is putting 00:00...

Select next event

I would like to select the next event from my table. the date of the event is stored as a VARCHAR in the format Y-m-d in a column called date.
But i am unsure how to compare VARCHARS/strings in sql. I tried the following but it gives me an event in the past.
$d = date("Y-m-d");
mysql_query("SELECT * FROM events WHERE date>$d ORDER BY date ASC LIMIT 1,1");
You haven't quoted your date string, so your query will be literally somethign like
... AND date > 2014-04-11 ORDER ...
Since there's no quotes, MySQL is free to interpret your date as a simple mathematical subtraction, and you end up doing
... AND date > 1999 ORDER ...
Try
... AND date > curdate() ...
instead. There's no point in having PHP generate a date and passing it into mysql, when mysql can generate that date perfectly well on its own.
As well, having PHP generate the date can lead to race conditions. E.g. PHP generates "today 11:59:59pm", but mysql actually executes as "tomorrow 00:00am". Maybe not relevant to you, but in banking "overnight run" code, this could cost someone literally millions of dollars.
SELECT * FROM events WHERE active=1 AND DATEDIFF(date, CURDATE()) >= 0 ORDER BY date ASC LIMIT 1,1;
This will select the next event inclusive of today's events. IF you do not want to include today's events, change >= to >
sqlfiddle: http://sqlfiddle.com/#!2/f205a3/9

Find the time difference between two datetime columns stored as varchar

I have a table column named date_created where I store datetime values in VARCHAR e.g. 16-06-2013 10:49:29
I want to get results through mysql query between tow dates. I am using query like this:
... WHERE date_created BETWEEN '06-08-2013 22:30:18' AND '28-08-2013 22:30:22' ...
This query return results but that result also includes older date records that are between 06 and 28 of every month. I also use < and > but these also did not work.
How can I get results that only include records between '06-08-2013 22:30:18' AND '28-08-2013 22:30:22'
You can use MySQL-function STR_TO_DATE to format strings into DATETIME.
SELECT STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s');
# 2013-08-06 22:30:18
I would recommend to reformat all VARCHARs to DATETIMEs with a single UPDATE and fix the code.
you need to use the STR_TO_DATE function to convert your date string into a date that can be used for comparisons.
STR_TO_DATE(date_created, '%d-%m-%Y %H:%i:%s') BETWEEN STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s') AND STR_TO_DATE('28-08-2013 22:30:22', '%d-%m-%Y %H:%i:%s')

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');

Categories