What is the equivalent of this function of php in mysql:
$cba=date_diff($cb,$ca);
$cba->format('%R%a.%h');
output: Date: 1.12 //that's 1day and 12 hours
Here's where I'm going to use it for:
DATEDIFF(q2.adate, q1.deact) AS ND,
Can i do the same process in mysql alone, Thanks
Try this using TIMEDIFF
SELECT CONCAT(
FLOOR(HOUR(TIMEDIFF(q2.adate, q1.deact)) / 24), ' days, ',
MOD(HOUR(TIMEDIFF(q2.adate, q1.deact)), 24), ' hours, '
)
AS ND
MySQL TIMEDIFF() returns the differences between two time or datetime
expressions. It is to be noted that two expressions must be of same
type.
From MySQL Docs:
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
You can use TIMEDIFF instead:
TIMEDIFF() returns expr1 − expr2 expressed as a time value. expr1 and expr2 are time or date-and-time expressions, but both must be of the same type.
So the result of TIMEDIFF is like:
-00:00:00.000000 // Negative times
00:00:00.000000 // Positive times
or TIMESTAMPDIFF:
TIMESTAMPDIFF() returns datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part '00:00:00' where necessary. The unit for the result (an integer) is given by the unit argument. The legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function.
So:
SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
will return 128885 (minutes).
See more about MySQL Date and Time Functions
Related
I am tring to convert time format like 12:00:00 to seconds by using MySQL function. I got message say You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':00:00. What can I do? Is there format cannot be converted? Please check my code
This works
SELECT TIME_TO_SEC(TIMEDIFF(2014-02-21 12:00:00,2014-02-21 13:00:00)) AS TimeInSecs
FROM Tbl_Time
This not working
SELECT TIME_TO_SEC(TIMEDIFF(12:00:00,13:00:00)) AS TimeInSecs
FROM Tbl_Time
You need to put single quotes ' with date and time
SELECT TIME_TO_SEC(TIMEDIFF('12:00:00','13:00:00'));
SELECT TIME_TO_SEC(TIMEDIFF('2014-02-21 12:00:00','2014-02-21 13:00:00')) AS TimeInSecs ;
TIMEDIFF() returns expr1 – expr2 expressed as a time value. expr1 and
expr2 are time or date-and-time expressions, but both must be of the
same type.
Have a look how TIMEDIFF works.
Try this with ' single quotes
SELECT TIME_TO_SEC(TIMEDIFF('2014-02-21 12:00:00','2014-02-21 13:00:00')) AS TimeInSecs ;
SELECT TIME_TO_SEC(TIMEDIFF('12:00:00','13:00:00'));
TIMEDIFF()
How can I add N more hours to MySQL datetime representation? For example
Current: 2013-12-01 19:30:13 (or I can get it with date("Y-m-d H:i:s") in PHP)
How can I get this: 2013-12-01 22:30:13 (adding 3 more hours)?
date("Y-m-d H:i:s") + 3 isn't working in PHP
in PHP:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours');
In MySQL you can use date_add
From the docs:
DATE_ADD(date,INTERVAL expr unit)
These functions perform date arithmetic. The date argument specifies
the starting date or datetime value. expr is an expression specifying
the interval value to be added or subtracted from the starting date.
expr is a string; it may start with a “-” for negative intervals. unit
is a keyword indicating the units in which the expression should be
interpreted.
The INTERVAL keyword and the unit specifier are not case sensitive.
For your case you can do:
date_add(now(), interval 3 HOUR)
sqlfiddle demo
It depends on where you want to do it.
in mysql
SELECT NOW() + INTERVAL 3 HOUR
SELECT CAST('2013-12-01 23:49:09' AS DATETIME) + INTERVAL 3 HOUR
in php
Date("Y-m-d h:i:s", time() + 3600 * 3)
PROBLEM HAS BEEN SOLVED
ANSWER:
delete from `[table name]` where [row name] > AddTime( CurTime(), '14400 hour' )
This instantly deletes anything that is older than 10 days OLD.
first change string into DATETIME with help of STR_TO_DATE
It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME
SELECT STR_TO_DATE('Wed Nov 21 2012', '%a %b %d %Y' )
// return 2012-11-21
then use DATEDIFF
DATEDIFF(expr1,expr2)
DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other.
SELECT DATEDIFF(CURRENT_TIMESTAMP(),STR_TO_DATE('Wed Nov 21 2012','%a %b %d %Y'))
//return 8
so complete query will be
$sql="DELETE FROM `journal`
WHERE DATEDIFF(CURRENT_TIMESTAMP(),
STR_TO_DATE('journal_date','%a %b %d %Y')
) > 5";
Note : in STR_TO_DATE function
use %d if you store day of month part with leading Zero ( 01..31 )
OR use %e if you store day of month part without leading zero (1..31)
MySQL date/time functions all work on MySQL DATE and DATETIME values of the format:
2012-11-28
2012-11-28 22:16:00
If you are actually storing your dates in the format you're specifying, MySQL cannot parse that.
For information on MySQL's date/time functions, see: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
You Can Use cron jobs to run specified script
delete from journal where journal_date < date_sub(curdate(),interval 5 day)
it can use index on column journal_date
but "datediff(now(), journal_date) > 5 " can't use index on column journal_date
Apparently the order of the dates matters. According to the MySQL reference for datediff() your dates are backwards, so you are probably only getting negative numbers (hence never more than 5, so no deletions).
Use abs() mathematical function to ignore negative date diffrence which is causing the problem apparently.
$sql="delete from `journal` where
abs(datediff(now(), STR_TO_DATE('journal_date', '%a %b %d %Y'))) > 5"
I have two query conditions here, and I would think that these queries would result in the same data, but they don't.
// 1st Condition:
WHERE month(jurnal.time) >= '01'
AND month(jurnal.time) <= '06'
AND year(jurnal.time) = '2012'
// 2nd Condition:
WHERE jurnal.time > '2012-01-01'
AND jurnal.time <= '2012-06-30'
I want the first condition to result in the same data as the second conditions.
Thanks in advance.
Your first version includes jurnal.time = '2012-01-01', whereas your second condition does not (owing to the use of > rather than >=).
It isn't clear from your question what is the data type of the jurnal.time column:
if it's DATETIME or TIMESTAMP, your second example will not have included any time after 00:00:00 on June 30 so one must instead either take only the date part:
WHERE DATE(jurnal.time) BETWEEN '2012-01-01' AND '2012-06-30'
or else ensure that the comparisons include all times of the day:
WHERE jurnal.time BETWEEN '2012-01-01 00:00:00' AND '2012-06-30 23:59:59'
if it's DATE, this point will not have affected your examples but one can nevertheless simplify your second example to:
WHERE jurnal.time BETWEEN '2012-01-01' AND '2012-06-30'
I recommend the BETWEEN comparison operator with CASTs:
WHERE jurnal.time BETWEEN CAST('2012-01-01' AS DATE) AND CAST ('2012-06-30' AS DATE)
Be sure to cast the stings to the same type as the column of jurnal.time.
Your conditonals in the first query are trying to compare as if they were numbers the values returned from the date parsing functions, but i think they are just strings. '01' and '06' are interperted by sql as plain strings, not the numbers 1 and 6.
The second is sql is smart enough to convert the full date string representation into a date to use in the comparison.
I had to do some date comparision and return a dataset. PHP sent the current date time, with a time with non leading zeros (8:00:00) for 8 am instead of (08:00:00). The case where there were no leading zeros, was giving wrong results. Can someone explain why ?
To test, run this SELECT IF(DATE_ADD('2011-03-20', INTERVAL '08:05:00' HOUR_SECOND) >= '2011-03-20 8:00:00',"yes","No")
result: No
AND
SELECT IF(DATE_ADD('2011-03-20', INTERVAL '08:05:00' HOUR_SECOND) >= '2011-03-20 08:00:00',"yes","No")
result: Yes
shouldn't both give the result : "Yes"
Is it doing string comparison for a non leading zero ?
I think this is to do with MySQL's rather dodgy date / time handling in som ecases MySQL converts to a numeric and in other cases it uses string comparisons.
From the manual
http://dev.mysql.com/doc/refman/5.0/en/using-date.html
When you compare a DATE, TIME, DATETIME, or TIMESTAMP to a constant string with the <, <=, =, >=, >, or BETWEEN operators, MySQL normally converts the string to an internal long integer for faster comparison (and also for a bit more “relaxed” string checking). However, this conversion is subject to the following exceptions:
When you compare two columns
When you compare a DATE, TIME, DATETIME, or TIMESTAMP column to an expression
When you use any other comparison method than those just listed, such as IN or STRCMP().
For these exceptional cases, the comparison is done by converting the objects to strings and performing a string comparison.
Strictly speaking, you are comparing against strings (not dates) and relying on automatic casting. Try this instead:
SELECT
IF(DATE_ADD('2011-03-20', INTERVAL '08:05:00' HOUR_SECOND) >= STR_TO_DATE('2011-03-20 8:00:00', '%Y-%m-%d %H:%i:%s'), 'Yes', 'No'),
IF(DATE_ADD('2011-03-20', INTERVAL '08:05:00' HOUR_SECOND) >= STR_TO_DATE('2011-03-20 08:00:00', '%Y-%m-%d %H:%i:%s'), 'Yes', 'No')
Update:
According to the manual, DATE_ADD() can return a date or a string:
The return value depends on the
arguments:
DATETIME if the first argument is a DATETIME (or TIMESTAMP) value,
or if the first argument is a DATE and the unit value uses HOURS,
MINUTES, or SECONDS.
String otherwise.
To ensure that the result is DATETIME,
you can use CAST() to convert the
first argument to DATETIME.
So the left operand of the >= comparison is a string thus you're getting string comparisons. Try the CAST('2011-03-21' AS DATE) suggestion.