Get data between two dates with date format unkown - php

date format: init(11)
date column value: 1421382119
I have managed to get the dates from the table.
$sqllast = $Db1->query("SELECT * FROM table");
while($row = $Db1->fetch_array($sqllast))
{
$date1 = date('Y-m-d', $row['create']);
echo "$date1";
}
Question:
I want to get values from this table using the dates interval. I am not getting idea which format shall I enter the dates.
I have tried y-m-d, d-m-y , but it did not work
I tried this query
SELECT * FROM table WHERE create between '2015-01-1' and '2015-01-30'

Since the column type is int, you have to convert your string dates to ints as well.
Try this:
SELECT * FROM `table` WHERE `create` BETWEEN UNIX_TIMESTAMP('2015-01-01') AND UNIX_TIMESTAMP('2015-01-30')

Since you are storing a unix_timestamp as your value in column create, you will want to use MySQLs UNIX_TIMESTAMP() to covert your dates to a unix_timestamp
SELECT * FROM table WHERE create between UNIX_TIMESTAMP('2015-01-1') and UNIX_TIMESTAMP('2015-01-30')`?

Related

How to compare date when its save as type text in database

I am using WPDB and here is my SQL
$date = date('d-m-Y');
$reservations = $wpdb->get_results( "SELECT * FROM reservation_db WHERE `date` > '$date'");
I need to select the date in my database when the date in database greater than today.
My date format is dd-mm-yyyy, but I think because it's save in text, it only compares days(dd) which is wrong, any solution to solve this?
MySQL offers a STR_TO_DATE function to convert a date string to date:
SELECT * FROM reservation_db WHERE STR_TO_DATE(`date`) > '$date'
But as ankit suthar mentioned in above comment, it is not recommended to store dates as text.

How to query a table based on timestamp with date in dd-mm-yyyy format

I have a table containing a field "created_at" whose Data Type is timestamp. I donot want to change it to DATE.
Can i query this table to fetch all rows of a day in format dd-mm-yyyy.
Note:
One approach I tried is:
a) Take Date in yyyy-mm-dd concatenate with 00:00:00
b) Take next date in yyyy-mm-dd concatenate with 00:00:00
and use below query to get all records of that day:
SELECT *
FROM news
WHERE created>='2016-12-13 00:00:00'AND
created < '2016-12-14 00:00:00'
Is this a good solution to my problem. Any better approach for this problem.
You can use the MySQL cast() function.
SELECT
*
FROM news
WHERE CAST(created_at AS DATE) = '2016-12-13'
This will discard the time component of your timestamp and do the comparison on only the date.
Reference: http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html
If you don't want to change column datatype then you can go with this code
$startDate = strtotime( '2016-12-13' ); // it will convert date to timestamp format.
$endDate = strtotime( '2016-12-15');
$query = "SELECT * FROM news WHERE created >= '$startDate' AND created < '$endDate'";

How to fetch record by month in MySQL?

I have stored Date in database in dd-mm-yy format, for example 03-10-2013,
How to search record by month? Month in digit (01 to 12);
I am using currently
$query = "SELECT * FROM data WHERE date LIKE %$month%";
but this not working properly.
I am assuming when you say dates as stored in the database in a format, that they are not stored using a "date" type and instead are using a varchar or char type for the column.
Based on that there are few ways to do this.
Leave the database as it is and convert values on the fly.
SELECT * FROM data WHERE Month(STR_TO_DATE(datestrcolumn, '%d/%m/%Y')) = 5;
Change the type of the column to a "date" type column
SELECT * FROM data WHERE Month(realdatecolumn) = 5;
Change the type of the column to a "date" type column, store a separate column for the month.
UPDATE data set monthcolumn = Month(realdatetimecolumn)
then
SELECT * FROM data WHERE monthcolumn = 5;
Create an index on monthcolumn and this query will be much faster than the other queries if there is a lot of data
Fix the date format in your database structure first, change it to: yyyy-mm-dd
Then change your query statement to:
$query = "SELECT * FROM data WHERE MONTH(`date`) = '$month';
This will select the month as '5' or '11' or '12' which will give duplicates for differing years.
If you need the month with year (to avoid duplicate years):
$query = "SELECT * FROM data WHERE SUBSTR(DATE(`date`),1,7) = SUBSTR(DATE('$month'),1,7);
This will return: '2015-01' or '2014-12'
To get date as '01' or '04' or '12':
$query = "SELECT * FROM data WHERE SUBSTR(DATE(`date`),6,2) = SUBSTR(DATE('$month'),6,2);
Try this...
You could use MySQL MONTH() function
MySQL MONTH() returns the MONTH for the date within a range of 1 to 12 ( January to December). It Returns 0 when MONTH part for the date is 0
4 is april
SELECT * FROM tbl WHERE MONTH( date ) ='4'
You can do like it... as it is not in date format(YYYY-MM-DD)
$q="SELECT * FROM data WHERE MONTH(DATE_FORMAT(STR_TO_DATE(date, '%d-%m-%Y'), '%Y-%m-%d') ) = '$YOUR_SEARCH_MONTH' ";

Using Date functions to pull records from database

I'm truly stumped on something - I have a table in my database with a column called 'today' that has Date and Time records. The column has entries that look like this:
October 25, 2014, 4:58 am
October 25, 2014, 4:36 am
I'm having trouble pulling the rows by date; I think the time stamp is messing with the MySQL query. And I need an SQL query to pull any records where the variable $today matches the date information in the column 'today'. This doesn't work:
$today = date("F j, Y"); // looks like this: October 25, 2014
$result = mysqli_query($link,"SELECT * FROM records WHERE today = $today"); // 'today' represents the column in the table
while($row = mysqli_fetch_array($result)) {
echo var_dump($row);
}
I just get an empty result, I think due to the time stamp. Can someone advise on a better MySQL query that will only grab the rows where $today matches the date in the 'today' column?
Although storing the date and time as string in varchar is not really a good idea, you could still alter your query to match string containing the current date with a LIKE statement:
$result = mysqli_query($link,"SELECT * FROM records WHERE today LIKE '$today%'");
That is just to get your current setup working as a temporary fix but i highly suggest you take a look at datetime and timestamp or similar date types if this is a serious project and not just playing around. with programming.
UPDATE
With a datetime you could get the dates which are the same as today with:
SELECT * FROM `records` WHERE `today` = CURDATE();
with a timestamp you would need to pass it as date so your query would be:
SELECT * FROM `records` WHERE date(`today`) = CURDATE();
You can just use the MySQL date functions:
SELECT *
FROM records
WHERE today = CURRENT_DATE;
If there is a time component on the today column, then the best structure is:
SELECT *
FROM records
WHERE today >= CURRENT_DATE and today < date_add(CURRENT_DATE, interval 1 day)
It's obvious that both dates are not equal. Both dates are treated like text values and are not equal. You need to convert the column containing date in your MySQL query as such:
$result = mysqli_query($link,"SELECT * FROM records WHERE DATE_FORMAT(today, '%F %j, %Y') = $today");
Note that you have to change your column to store values of the type of DATE. Or just use queries as proposed in other answers.

How do I select a row based on a date

I have row in which is string value from strtotime(), for example 1303448400.
My table has following stucture:
id | date
And from my input I recive date in this format: MM/DD/YY.
How to create a query in SQL which will select id where date is greater than 11/11/13?
You could convert your date (11/11/13) to a timestamp before using it in a query, using mktime():
http://php.net/manual/en/function.mktime.php
Use strtotime as you said,
SELECT * FROM TABLE WHERE DATE > strtotime($yourDate)
$date=strtotime('11/11/13');
SELECT * FROM TABLE db_date DATE > $date
This is how it will work for you.
$date_1 = 11/11/13; // your date
$date_2 = strtotime($date_1); //change it to strtotime
$query = mysql_query("SELECT id FROM table WHERE date DATE > '".$date_2."'"); //select id which is greater
Change the variables and rows to fit yours.

Categories