Dates are stored in DB as such: "2011-05-26 11:00:00" in a datetime field. I'd like to find all rows where the date is greater than the first of this month, ie: 2011-05-01 09:00:00
The date formats in the DB can not be changed. What MySQL function can I use to convert the date in the DB to a format that can handle comparison? I'm guessing the best format for "first of the month" is a unix timestamp?
The time values in the dates are always present but only office hours, so from 09:00 to 17:00.
If it's stored in a DATETIME field, just query on WHERE date > '2011-05-01 09:00:00'.
$firstDayOfMonth = date( 'Y-m-d H:i:s', mktime(0,0,0,date('n'),1,date('Y'));
$query = "SELECT * FROM `table` WHERE `date` >= '$firstDayOfMonth'";
Something like this perhaps?
$oDateTime = new DateTime();
$sQuery = "SELECT * FROM table WHERE date >= ".$oDateTime->format("Y-m")."-01 09:00:00";
Compare to the datetime directly:
WHERE date_field >= '2011-05-01 00:00:00'
DATETIME fields can filtered just like integer fields, example:
SELECT * FROM `table` WHERE `date` > '2011-05-01 09:00:00'
In case you really want to convert to Unix timestamps, have a look at the UNIX_TIMESTAMP function.
You can use TIMESTAMPDIFF function in MySQL to compare time,
FROM_UNIXTIME function to format Unix timestamp as date.
More mysql data and time functions and examples are available in MySQL Reference Manual date and time function .
I think that you can solve it, something like this:
firstly, create first day of month in mysql format in php
$firsDay = date('Y-m-d 00:00:00', strtotime(date('Y-m').'-01 00:00:00'));
and then, use it in the query
select * from something where date >= $firsDay
Related
I get a datetime field, that's currently in the query as:
SELECT DATE_FORMAT(x.date_entered, '%Y-%m-%d') AS date FROM x ORDER BY date ASC
What I want to do is to subtract 3 hours from that date (GMT issues), but I can't do it in PHP as PHP only knows the date part, not the time.
mySQL has DATE_SUB():
SELECT DATE_SUB(column, INTERVAL 3 HOUR)....
but would it not be better to try and sort out the underlying time zone issue instead?
Assuming you have some timezone issue and know source and destination timezone, you could convert it like so
SELECT DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'),
'%Y-%m-%d') AS date
FROM x ORDER BY date ASC;
Normal select query.
Once applied DATE_ADD() function in MySQL
select lastname,
date_add(changedat, interval -24 hour) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.
I am trying to select everything from my table with today's date. But I found that that date column is in unix time stamp. So, how do I select everything with today's date? or for example only yesterday's ? If it was normal date instead of unix time it would be easy, but... here is what I have put for my query so far..
$ann_renewal_query = "SELECT * FROM annual_renewal WHERE due_date '%$today%' order BY due_date ASC";
You just need to convert the values to the unix timestamp version. The best way is to convert the current time to Unix timestamp:
where due_date >= UNIX_TIMESTAMP(curdate()) and
due_date < UNIX_TIMESTAMP(date_add(curdate(), interval 1 day)
This version of the query allows it to take advantage of an index on due_date.
Can anyone tell me where's my mistake? By using that query, it should return some rows where the data have datetime = '2012-10-12' right? Here is my reference
My datetime column = 'YYYY-MM-DD HH:MM:SS', data type = datetime.
I am using XAMPP v1.8.0, MySQL v5.5.25a.
Try CASTing datetime to date by using DATE()
SELECT *
FROM tableName
WHERE DATE(`datetime`) = DATE(CURDATE())
YYYY-MM-DD HH:MM:SS is not equal to YYYY-MM-DD
2012-01-01 12:12:12 is not equal to 2012-01-01 00:00:00
Do not use functions on your columns, e.g. DATE(datetime) - mysql can't use your index.
It's almost certainly better to use a range:
WHERE `datetime` between '2012-01-01 00:00:00' and '2012-01-01 23:59:59'
or store just the DATE portion in a separate column (which will have lower cardinality and be better treated by the optimizer).
SELECT * FROM tableName WHERE DATE(datetime) = CURDATE()
use DATE() from mysql to format the TIMESTAMP to DATE
Select * FROM tablename WHERE DATE(colname) = CURDATE()
MySQL docs about NOW() function...
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.
Here's my query...
$sql = "
INSERT INTO `users` ( `username`, `password`, `email`, `time` )
VALUES ('{$username}', '" . sha1( $password ) . "', '{$email}', NOW() )
";
The problem is that in the database I don't have the full datetime, but only the year. Any solutions?
It's int(10) like it was when I used UNIX timestamp.
NOW() works for DATETIME fields only.
You need to either convert your field to DATETIME (the preferable method), or convert NOW() into a UNIX timestamp using UNIX_TIMESTAMP():
UNIX_TIMESTAMP(NOW())
I had this same trouble and was stumped for the longest time till I read this question. I used mysql's now() function to update a member's last login time.
MySQL's now() returns the date time as a string, like YYYY-MM-DD HH:MM:SS - somewhere along the line I stopped using this and started using time() (UNIXTIME) (makes it easier to work with) which returns a string of numbers, like 1352254528. But you can't store a string of numbers like this in a datetime field, and likewise, you can't store YYYY-MM-DD HH:MM:SS in an int(11) field. The latter will result in only the year being stored.
In the end you should use:
int(11) if you're going to use PHP's time() function to store the time as a string of numbers
or
DATETIME field if you're going to use MySQL's NOW() function.
try
strtotime(now())
And you should definitely use timestamp or datetime as a type for your column.
My table is using a datetime (YYYY-MM-DD HH:MM:SS) and i need to display today's entries.
my code is only :
SELECT *
FROM table
WHERE date = '$date'
ORDER BY score DESC
with
$date = date("Y-m-d");
well, as expected it doesnt work :| you guys have a solution here ?
Following from Pascal Martin, you could extract the date part from the date+time field:
SELECT * FROM table WHERE DATE(date) = '2009-12-19'
Source: MySQL - Date and Time Functions
Be aware however, that this query will not use an index on your date+time field, if you will be having one. (Stack Overflow: How does one create an index on the date part of DATETIME field in MySql)
Your date is "2009-12-19" (or something like that, depending on the day), which is interpreted as "2009-12-19 00:00:00".
In your database, you probably don't have any date that's exactly equal to that one, by the second : your dates are like "2009-12-19 12:15:32".
A solution is to compare like this :
select *
from table
where date >= '2009-12-19'
and date < '2009-12-20'
Which will be interpreted as :
select *
from table
where date >= '2009-12-19 00:00:00'
and date < '2009-12-20 00:00:00'
And, if you don't want to do the math to get the date of the following date, you can use the adddate function :
select *
from table
where date >= '2009-12-19'
and date < adddate('2009-12-19', interval 1 day)
So, in your case, something like this should do the trick :
select *
from table
where date >= '$date'
and date < adddate('$date', interval 1 day)
order by score desc
You probably want to format the data when you select it:
SELECT *, DATE_FORMAT(date, '%Y-%m-%d') AS dateformat FROM table
WHERE dateformat = '$date' ORDER BY score DESC
You are comparing datetime and date expression, that is why its not working. Use Date() method to return the date part from datetime and then do the comparison. WHERE DATE(date) = '$date' should do. You might have to use aliases to handle this name collision.