How do I select a row based on a date - php

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.

Related

How to convert varchar data type into datetime data type without data lose

my query(select invoice_date from pr_tax_intra where invoice_date between '03-05-2018' and '05-05-2018')
invoice_date data type varchar->(invoice_date varchar(55) )
output images:
enter image description here
enter image description here This images select only 5 month data but show in 4&5 month data also ...
i am using mysql database
i have stored date as varchar(255) data type and Date format is dd/MM/yyyy now i have to convert it into date data type can u help me in query ?
i have 780 records and each recod contain the Date in format of dd/MM/YYYY but due to varchar data type i can not perform calculation on Date so i have to convert into date without data lose.
please help me
You can use myqsl STR_TO_DATE() function
SELECT STR_TO_DATE("10 August 2017", "%d %M %Y")
//For Testing
SELECT DATE_FORMAT(STR_TO_DATE('21/05/2018', '%Y/%m/%d')
,'%d/%m/%Y')
In your case use below query
SELECT DATE_FORMAT(STR_TO_DATE(YourVarcharDateColumn, '%Y/%m/%d')
,'%d/%m/%Y') from YourTable
You can do that like this,
get all your record and then apply a while loop
$ListContent = mysqli_query($conn, "SELECT `date_filed` FROM table_name") or die(mysqli_error($conn)." Error in query"); // $conn is connection object
while($arr = mysqli_fetch_array($ListContent)){
$currentDate = $arr['date_filed'];
$currentRowID = $arr['id']; // Unique id column name
$newDate = date("Y-m-d", strtotime($currentDate));
$sqlUpdate = "Update table_name SET `date_filed` ='".$newDate."' WHERE id=".$currentRowID;
mysqli_query($conn, $sqlUpdate) or die(mysqli_error($conn)." Error in update query");
}
This will make your all record in "yyyy-mm-dd" format and then change your field into date type
SQL Server (MSSQL)
SELECT FORMAT(YourDateColumn,'dd-MM-yyyy') from yourTable

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

Get data between two dates with date format unkown

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')`?

convert Unix Time stamp and sort data in mysql query

I am trying to query a small database by date, my date table data is stored in time 2014-02-04 . how can I convert that and check it against todays date.
This is what I have but I am getting a few errors
$q = 'SELECT count(*) as count FROM SHOW WHERE date('Y-m-d', strtotime
('SHOW_DATE') ='.$db->qstr(date()).' AND CONTACT='.$db->qstr($name);
if(!$rs = $db->execute($q)){
force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1');
exit;
} else {
$today_count = $rs->fields['count'];
$smarty->assign('today_count',$today_count);
}
Thanks a lot.
You can convert show_date to a date format using FROM_UNIXTIME function. And then compare the date part of it with your input date value.
Example:
SELECT count(*) as count FROM `SHOW`
WHERE date( from_unixtime( `SHOW_DATE` ) ) = ? AND CONTACT=?
Use prepared statement to bind input values to the place holders.
To find a row containing a DATE matching the current date, use CURDATE():
SELECT column FROM table WHERE col_date = CURDATE()

Mysql query: retrieve current date query

In mysql database i have this column called:
Name: Date
Type: datetime
I have few values in that column:
2009-01-05 01:23:35
2009-03-08 11:58:11
2009-07-06 10:09:03
How do I retrieve current date? I am using php.
in php:
<?php $today = date('Y-m-d');?>
How to write a mysql query to retrieve all today date data?
Should i change the column type to "date", then insert values like "2009-07-06" only, no time values???
You don't need to use PHP, MySQL has a function to get the current date:
SELECT field FROM table WHERE DATE(column) = CURDATE()
Documentation: CURDATE, DATE.
If your column is only ever going to need the date part and never the time, you should change your column type to DATE. If you insist on doing it through PHP, it is the same thing, really:
$today = date('Y-m-d');
$query = mysql_query("
SELECT field FROM table WHERE DATE(column) = '$today'
");
For date time it is not usefull, instead I try this and working...
Today's Visitors!
sql > select user_id from users where last_visit like concat('%' , CURDATE() , '%');
// last_visit coloumn of type 'datetime'

Categories