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'
Related
I am trying to select an unixtimestamp column from my database, as readable date time:
SELECT count(*) FROM users WHERE user_by="Admin" AND expire(FROM_UNIXTIME(unixtime),'%Y-%m-%d')='2015-10-02'
Above gives me this error:
#1305 - FUNCTION database_maindb.expire does not exist
How can I select the unixtimestamp from column expire as datetime, in the format: year-month-date?
assuming your database field is called "expire":
SELECT count(*) FROM users WHERE user_by="Admin" AND FROM_UNIXTIME(expire,'%Y-%m-%d')='2015-10-02'
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime for details
You don't need to select is as the YMD date stamp, you could do something such as (using your tags)
$query = "SELECT * FROM `table`;";
$result = pdoObject->queryMethod($query);
$result= pdoObject->fetchAssocMethod($result);
$date = date("Y-m-d", $result['epochCol']);
I hope this helps.
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.
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()
I have a database that contains a column with type - Date. I also have a query with the date inputted as static which works fine but I would like to use todays date in the query. any recommendations?
Query :
$q = 'SELECT count(ID) as count FROM ORDER WHERE
ASSIGN_TO ='.$db->qstr($person).' AND OPEN_DATE ='.$db->qstr('2014-05-14');
This currently displays count of items after 2014-05-14
You could use the NOW() function that returns the current date. To avoid skewed answered by hours/minutes/seconds, you can use date to extract the date part:
$q = 'SELECT count(ID) as count FROM ORDER WHERE
ASSIGN_TO ='.$db->qstr($person).' AND DATE(OPEN_DATE) = DATE(NOW())';
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.