I'm having trouble with MySQL format_date, and don't understand why. I have the following as part of my code:
date_format(NOW() + INTERVAL 3 DAY, '%Y-%m-%d')
which seems to work fine, except for the fact that regardless of the date I choose, the %d is returning as a single zero ('0'). If I change %d to %e I can get the correct date, but I'm using this to compare dates, so I need the leading zero for numbers below 10. Is this a database setting, or am I missing something obvious?
Thanks in advance.
UPDATE: I feel like it has to be something in the db, because when I simplify the query to this:
$q = "SELECT date_format(NOW() + INTERVAL 3 DAY, '%Y-%m-%d') as 'today' from content_field_date LIMIT 1";
'today' prints out as '2012-03-0'
Meanwhile, this:
$q = "SELECT date_format(NOW() + INTERVAL 3 DAY, '%Y-%m-%e') as 'today' from content_field_date LIMIT 1";
correctly returns '2012-03-17'
Zeth
I ran into this same issue today, based on your table name of "content_field_date" I'm assuming Drupal was being used. The answer to this can be found here:
Some sql queries work in terminal but not with db_query() function in Drupal 6 Why..?
and here:
http://drupal.org/node/100846
in that the percent signs need to be escaped, so the correct query should be:
$q = "SELECT date_format(NOW() + INTERVAL 3 DAY, '%%Y-%%m-%%d') as 'today' from content_field_date LIMIT 1";
WHat you describe should not happen. You either found a MySQL bug or you are doing something wrong.
If you only want to compare dates, you can do that inside MySQL and you probably shouldn't be using DATE_FORMAT() at all. You can use this to get a date:
DATE(NOW() + INTERVAL 3 DAY)
or:
(CURDATE() + INTERVAL 3 DAY)
Related
I'm trying to use sql CURDATE(), in format "YYYY-MM-DD", to select items from yesterday but time column is in "YYYY-MM-DD HH-MM-SS" format.
Is it possible to put CURDATE between % %, something like in this example (which obviously doesn't work);
SELECT * FROM table WHERE created_at LIKE % CURDATE() - INTERVAL 1 day %
Or if there is no syntax variation which makes use of curdate & also that works, do please recommend another way if there is any in sql bag of tricks.
Don't use string functions as strings! If you want yesterday, you can use:
where created_at >= current_date - interval 1 day and
created_at < current_date
Plus, this can use an index.
Or, you can simplify this to:
where date(created_at) = current_date - interval 1 day
And this version does not use an index on created_at.
I am trying to display the number of records for the current month for a specific user. Unfortunately, it does not work, because At one time, the date format in the Database was not correctly indicated.
The text column, the date is written in plain text format.
How to be? I tried a lot, nothing comes out.
I try STR_TO_DATE, convert and other - the output is either null or mysql error.
I also tried to manually enter a request into phpmyadmin. Unsuccessfully.
Mysql version: 5.6.39-83.1 (Linux)
$modercount = mysqli_query($link, 'SELECT moder, COUNT(*) FROM my_logs WHERE server="'.$profileid.'" AND MONTH(time) = MONTH(NOW()) AND YEAR(time) = YEAR(NOW()) GROUP BY moder ORDER BY COUNT(*) DESC') or die(mysqli_error($link));
Date format in my sql column: 10.04.2018 12:52:18
First of all, get your textually formatted dates translated correctly to DATETIMEs. STR_TO_DATE() uses the same format strings as its reverse, DATE_FORMAT().
SELECT STR_TO_DATE(`time`, '%Y.%m.%d %H:%i:%s')
Second, select the range of datestamps you need like this, using LAST_DAY() to handle month arithmetic.
WHERE STR_TO_DATE(`time`, '%Y.%m.%d %H:%i:%s')
>= LAST_DAY(CURDATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND STR_TO_DATE(`time`, '%Y.%m.%d %H:%i:%s')
< LAST_DAY(CURDATE) + INTERVAL 1 DAY
When you get a chance, add a new column to your table with the datestamps in DATETIME or TIMESTAMP format. That way your date-range selections can exploit indexes. How to do that is beyond the scope of your question today.
you should use str_to_date this way
select str_to_date('10.04.2018 12:52:18', '%d.%m.%Y %T')
This works for me.
$modercount = mysqli_query($link, 'SELECT moder, COUNT(*) FROM mylogs WHERE server="'.$profileid.'" AND MONTH(str_to_date(`time`, "%d.%m.%Y %T")) = MONTH(NOW()) AND YEAR(str_to_date(`time`, "%d.%m.%Y %T")) = YEAR(NOW()) GROUP BY moder ORDER BY COUNT(*) DESC') or die(mysqli_error($link));
Big thanks to #scaisedge and #o-jones
This is probably easy to do but I can't seem to get my head around it. I have a user table that has a next_payment DATETIME column which gets update every month. I would like a query to get all the users where their next_payment DATETIME is in one day from the current datetime.
I tried something like this but it also gets me users where their next_payment is due in like 15 minutes. Not good
SELECT * FROM users WHERE next_payment >= NOW() AND next_payment <= NOW() + INTERVAL 1 DAY
I also tried something like this but this doesn't work either as it gives me all users that had next_payment datetime like 2 or 3 months ago (Not good).
SELECT * FROM users WHERE next_payment <= NOW() + INTERVAL 1 DAY
Thanks in advance for any help
Need to Cast the datetime field to date so you can include all data within the period and use between
SELECT * FROM
users
WHERE
cast(next_payment as date)
between cast(NOW() as date)
AND cast(NOW() + INTERVAL 1 DAY as date)
DATEDIFF works fine in MySQL:
SELECT
*
FROM
users
WHERE
DATEDIFF(next_payment, NOW()) = 1
SQL Fiddle for this example.
I would use:
SELECT * FROM users
WHERE TIMESTAMPDIFF(HOUR,NOW(),next_payment) <= 24
I need to retrieve the data from table based on the time and date i.e I need to get the data from yesterday 5.00pm to current day till 5pm. I have tried the following code in which I retrieved the yesterday and today but with time it is not working help to solve this problem note:the column modified is in timestamp
SELECT *
FROM `mdl_forum_posts`
WHERE from_unixtime(modified, '%Y-%m-%d') = CURDATE()
OR from_unixtime(modified, '%Y-%m-%d') = date(CURDATE()-1)
SELECT * FROM `mdl_forum_posts`
WHERE modified BETWEEN UNIX_TIMESTAMP(
CONCAT(DATE_FORMAT(CURDATE()- INTERVAL 1 DAY, '%Y-%m-%d'), ' 17:00:00'))
AND UNIX_TIMESTAMP(
CONCAT(CURDATE(), ' 17:00:00'));
Note that CURDATE() is polymorphic; the DATE_FORMAT is required for the first predicate where the calculation forces an integer-like return type.
Wrapping attributes in functions within predicates disables the use of indexes - applying the inverse function to the literal allows the for use of indexes (and even in the absence of indexes will be faster as the function need only be evaulated once).
if the date 1st of the month CURDATE() - INTERVAL 1 DAY gives exact previous day date
SELECT * FROM `mdl_forum_posts`
WHERE modified BETWEEN UNIX_TIMESTAMP(
CONCAT(CURDATE() - INTERVAL 1 DAY, '%Y-%m-%d'), ' 17:00:00'))
AND UNIX_TIMESTAMP(
CONCAT(CURDATE(), ' 17:00:00'));
I need to SELECT all records that are 30 days old. I have the code below but it's not working. In updatestatus I have dates like 12/26/2011. I create a 30 day old date like
$onemonthago="01/01/2012";
$sth = $dbh->prepare(qq(
SELECT *
FROM people
WHERE STR_TO_DATE (updatestatus,'%m/%d/%y')
<= STR_TO_DATE ( "$onemonthago",'%m/%d/%Y')
) );
If the datatype of updatestatus is date:
SELECT *
FROM people
WHERE updatestatus <= '2012-01-01'
or:
SELECT *
FROM people
WHERE updatestatus <= CURRENT_DATE() - INTERVAL 1 MONTH
If the datatype is datetime or timestamp and you want to check the time part, too:
SELECT *
FROM people
WHERE updatestatus <= NOW() - INTERVAL 1 MONTH
You can put an exact datetime instead of the NOW() - INTERVAL 1 MONTH. The correct way depends on how you are storing the datetimes or timestamps (does the Perl code or MySQL creates them in the first place?).
You could also put - INTERVAL 30 DAY which yield slightly different results.
This is what I used. Very simple
$sth = $dbh->prepare(qq(SELECT * FROM people WHERE updatestatus + INTERVAL 30 DAY <= NOW() )) or die $DBI::errstr;
If the time column is in timestamp then use below query.(use from_unixtime function)
SELECT wd.* FROM `watchdog` as wd
WHERE from_unixtime(wd.timestamp) <= NOW() - INTERVAL 1 MONTH
You can try this way. In SQL, there is dateadd function and I think there should be similar function in MySQL.
select *
from Table
where str_to_date between dateadd(day,-30,getdate()) and getdate()
It retrieve records between current date and past 30 days. You need to adjust for time. If you don't count time, you need to remove timestamp.