I do not understand how to get output from my sql table .
the table date is stored in int . for example date output from one row is like 1362157869 .
i want to show today orders in query :
php : $today = date("y-m-d", time());
query : SELECT * FROM test WHERE date = '$today'
but it didn't work . i also try this :
SELECT * FROM test WHERE date LIKE '$today'
Query without PHP var:
SELECT * FROM test WHERE date = DATE(NOW());
You are storing your date as a UNIXTIMESTAMP, so you have to convert it to DATETIME using FROM_UNIXTIME, and then you have to extract only the date part, ignoring the time, using the DATE() function:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
Please see fiddle here. You can then extract yesterday records with something like this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE() - INTERVAL 1 DAY
Or a specific date with this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = '2013-03-01'
To make use of an index
Assiming that your date column could contain not only the date part but also the time, you could also use this that will return all today records:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE())
and this for yesterday:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 1 DAY)
AND date < UNIX_TIMESTAMP(CURDATE())
or this for a specific date:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP('2013-03-01')
AND date < UNIX_TIMESTAMP('2013-03-01' + INTERVAL 1 DAY)
Related
I have a varchar field contain a timestamp value, i want to get the tomorrow records.
This is my code as you can see:
$tomorrow = strtotime("1+ day");
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow
but the query is incorrect
thanks for the help in advance
Should be like this :
$tomorrow = strtotime("+1 day");
/* this will select all record before tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date < $tomorrow;
/* this will select all record after tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow;
http://php.net/manual/en/function.strtotime.php
SELECT * FROM tasks WHERE task_accomplish_date > NOW() + INTERVAL 1 DAY
You should be able to do this all in the MySQL query
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) = DATE(NOW() + INTERVAL 1 DAY)
That converts your task_accomplish_date into a date value and looks for records where that date is equal to today + 1 day (tomorrow). That does not give you records from 2 days from today or beyond, just tomorrow. If you want tomorrow and all records beyond tomorrow you would use
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) >= DATE(NOW() + INTERVAL 1 DAY)
If you are storing a timestamp, these only care about the date part of the timestamp, not about the time part of the timestamp. If you care about the time part as well you can remove DATE() from both sides of the = in the WHERE clause
I am having column DOB which have dob of person and store it in (y-m-d) format. i want to know what will be the query to get today's birth date.
I had tried date(y-m-d) but it only show today's date result.
i.e. if there is 2018-2-20 as dob in my db then it will show in my result but if there is 1995-2-20 this date is not shown as result so basically i just want to display who's birthday is today.
You can try like this
MySQL Query :
SELECT *
FROM
YourTable
WHERE
DATE_FORMAT(BirthDayField,'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')
This query convert your desired date and current date to m(month)-d(day) format and compare
Note : Here BirthDayField is column name with data type DATE
Details about MySQL DATE_FORMAT function
first get the current date.
$today = date("yyyy-mm-dd");
then init the query where you say what should be selected.
$query = sprintf("SELECT * FROM bla WHERE `DOB` = %s", $today);
I guess you try something like this, using a case statement to determine if a record is a current birthday
select *,
case when day( date( dob ) )=day( date( now() ) ) and month( date( dob ) )=month( date( now() ) ) then 1
else 0
end as 'birthday'
from table
You can write like this
SELECT *
FROM persons
WHERE DATE_ADD(birthday,
INTERVAL YEAR(CURDATE())-YEAR(birthday)
+ IF(DAYOFYEAR(CURDATE()) >= DAYOFYEAR(birthday),1,0)
YEAR)
BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 0 DAY);
I think this will help you to getting the users who have b'day today.
I have 2 columns one having month and another year , I want to get all the records greater than that month and year
I cannot use date functions as those columns are of integer type, It gives incorrect results
How can I get records greater than that month and year
For example How can I get events which took place after february 2017
What about this approach:
SELECT * FROM table WHERE CONCAT(year, month) > 201702
WHERE CONCAT(year, month) > 201702
probably you have also some php code inside so:
$date = strtotime('2018-01-01 01:01:01');
$sql = 'SELECT * FROM YOUR_TABLE
WHERE CONCAT(year, month) > ' . date('Ym', $date);
SELECT * FROM events WHERE CONCAT(year,month) >= 201702
or without CONCAT
SELECT * FROM events WHERE( month > 2 AND year =2017) OR (year > 2017)
$today = CURDATE();
$result = mysqli_query($con,"SELECT * FROM Persons WHERE Day ='"$today"'");
The columns are:
Name
Day
Time
Reg
And a select * from Persons works fine.
You should be able to just specify your current date in the query (i.e. no need to calculate in PHP). This would also give you more consistant time handling in case web server and MySQL server have different timezones.
If Day is datetime or timestamp field use this:
SELECT * FROM Persons WHERE Day LIKE CONCAT(CURRENT_DATE(),'%')
If Day is date field use this:
SELECT * FROM Persons WHERE Day = CURRENT_DATE()
If day is a datetime, then use:
SELECT *
FROM Person
WHERE Day >= CURRENT_DATE and Day < CURRENT_DATE + interval 1 day;
The use of like for dates is bad practice, because it requires converting the date to a string. This prevents an index from being used.
If day has no time component, the above will work, but you can simplify it to:
SELECT *
FROM Person
WHERE Day = CURRENT_DATE;
You can also write:
SELECT *
FROM Person
WHERE date(Day) = CURRENT_DATE ;
(The parentheses on CURRENT_DATE are optional.)
$today = CURDATE();
$result = mysqli_query($con,"SELECT * FROM Persons WHERE Day LIKE '$today'");
how would I generate a MySQL query to grab a row that is between a particular month
eg grab row where month is between 01/01 (1st Jan) and 01/06 (1st June)
I'm not sure, if you want to grab all rows from Jan to June, only the rows until June 1st or all rows from Jan to May. You can use for example:
[...] WHERE `date_column` BETWEEN '2012-01-01' AND '2012-06-01'
which gives you all rows including June 1st.
Use this to get all rows from the full months of Jan to June:
[...] WHERE YEAR(`date_column`)=2012 AND MONTH(`date_column`) BETWEEN 1 AND 6
(change to BETWEEN 1 AND 5 for Jan to May)
If you generate your SQL query in PHP, you can use BETWEEN
$date = date("Y") . '-' . date("m") . '-01';
$sql = "SELECT *
FROM `table`
WHERE `date_column` BETWEEN '$date' AND '$date' + INTERVAL 1 MONTH - INTERVAL 1 DAY";
(I subtracted one day to exclude the data of the next month first day)
Or you can use EXTRACT
$month = 5;
$sql = 'SELECT *
FROM `table`
WHERE EXTRACT(MONTH FROM `date_column`) = ' . $month;
Assuming the column in mysql is the right format (date/datetime) you can simply compare (>,<) :
SELECT somecolumn
FROM sometable
WHERE datecolumn >= '2012-01-01' AND datecolumn < '2012-06-01'
Hope it helps!
You can use the month(), year() and day() functions. So your query would look like something like
SELECT * FROM table WHERE month(date_field) >= 1 AND month(date_field) <= 6 AND year(date_field) = 2011
date_field should be a date or datetime field, if it is a timestamp, you need to use the FROM_UNIXTIMESTAMP function so it would look like month(FROM_UNIXTIMESTAMP(timestamp_field))
SELECT
*
FROM
yourtable
WHERE
date BETWEEN '2012-01-01' AND '2012-06-01';