Would this be possible? I've used this to insert the date into a field called "date":
$date=date("m/d/y");
$sql="INSERT INTO pool (date) VALUES('$date' )";
$result=mysql_query($sql);
I've used this statement to get the date a week ago:
$variable = date('d-m-y', strtotime('-1 week'));
So how would I SELECT any rows which were added last week?
Instead of storing your dates as m/d/y, you should store them as Y-m-d :
$date=date("Y-m-d");
$sql="INSERT INTO pool (date) VALUES('$date' )";
In the database, you dates will then look like 2011-04-09.
That format is much easier to work with : alphabetical comparisons will work.
Which means that searching for rows that are older than a certain date would become something like this :
$variable = date('Y-m-d', strtotime('-1 week'));
$query = "select * from pool where date < '$variable'";
Also note that instead of working with a date field which is a varchar (or an equivalent) in your database, you could use a DATE column -- which would allow to to work with date and time functions in MySQL.
If the date field is a proper date type you can do < or > in your sql query. For example -
SELECT * FROM table WHERE date > '$date'
If you want everything from 1 week ago to now you can do something like the above or
SELECT * FROM table WHERE date BETWEEN '$date' AND NOW()
Related
I have a column in MYSQL that stores the datetime like so:
2017-08-18 08:59:53
2017-08-18 07:59:00
I can search the database and return the results between the datetime column like so:
$from = date("Y-m-d H:i:s", strtotime($_GET['from']));
$to = date("Y-m-d H:i:s", strtotime($_GET['to']));
$sql1="SELECT * FROM `Table` WHERE mydate>='$to' and mydate<='$from'";
This works fine as long as I search like this:
2017-08-18 08:59:53
But what I need to be able to do is to search like this:
2017-08-18
and still get the same result when I search.
I don't want to use the time in my search string/query.
Try mysql date() function like:
$sql1="SELECT * FROM `Table` WHERE date(mydate)>='$to' and date(mydate)<='$from'";
mysql date() function returns only date part from datetime column in default Y-m-d format.
You are very close. If you want to search a DATETIME or other datestamp column for matches to a particular day, do this:
WHERE mydate >= DATE(whatever_first_day)
AND mydate < DATE(whatever_last_day) + INTERVAL 1 DAY
This searches for everything on the date mentioned in whatever_first_day, starting with midnight on that date, and everything up to, but not including (<), midnight on the day after whatever_last_day.
This way of searching is sargable: it can use an index on the mydate column.
You could also use
WHERE DATE(mydate) >= DATE(whatever_first_day)
AND DATE(mydate) <= DATE(whatever_last_day)
but that is not sargable.
"SELECT * FROM `Table` WHERE date_format(mydate,"%Y-%m-%d")>='$to' and date_format(mydate,"%Y-%m-%d")<='$from'";
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'";
I am trying to do, what I assume is, an easy task of adding days to a date.
I have a date stored in a MySQL table, in a column called meta_date, with the type of DATE (A date, supported range is 1000-01-01 to 9999-12-31)
I retrieve this date from the database as follows:
$thisId = 1;
$dateQuery = mysqli_query($connection, "SELECT * FROM `sometable` WHERE `id` = '$thisId'");
$fetchDate = mysqli_fetch_assoc($dateQuery);
$theDate = $fetchDate['meta_date'];
Now I add a number of days to this date.
$newDate = date("Y-m-d", strtotime($theDate . " + 7 days"));
Next I put it back inside the database with an UPDATE query.
$editDate = mysqli_query($connection, "UPDATE `sometable` SET `meta_date` = '$newDate' WHERE `id` = '$thisId'");
However the date always returns as 0000-00-00 after the update.
Am I missing something here to do with the way the date is handled in PHP?
edit: The data I first retrieve from the database (into $theDate) is "2016-11-30".
You can use Mysql's built in function DATE_ADD()
Syntext
DATE_ADD(date,INTERVAL expr type) Where date is a valid date expression and expr is the number of interval you want to add.
For your case
UPDATE sometable
SET `meta_date` = DATE_ADD(`meta_date` , INTERVAL 7 DAY)
WHERE `id` = '$thisId';
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.
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'