MySQL - Cannot convert time format to seconds - php

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()

Related

Equivalent of PHP format('%R%a.%h'); in MySQL

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

SQL query error using to_char

The last line of my query will not run without "not a valid month" error. I am trying to convert a start time in GMT to local time using an offset in one of the columns, then compare this to a date I have in a variable. It works without using the to_char and offset.
Any insight would be appreciated.
AND to_char(CT.START_GMT + K.OFFSET/1440,'MM/DD/YYYY HH24:MM:SS') >= TO_DATE('$From_Date', 'mm/dd/yyyy')
AND CT.START_GMT + K.OFFSET/1440 >= TO_DATE('$From_Date', 'mm/dd/yyyy')
You don't need a TO_CHAR() conversion.. if CT.START_GMT is a DATE
If CT.START_GMT is not DATE/TIMESTAMP , then you need to do TO_DATE() with the right format.

SQL Error on date, whats the correct format to upload seperate date and time?

I want to know what the problem with my PHP code is and why SQL is throwing this error.
The date and time format look fine in comparison to other articles I have looked at.
Error
insert story failed: 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 ':21:20, planning)' at line 2
PHP
$insertStoryData = "INSERT INTO `story` (story_name, story_text, story_point, uploader_name, story_date, story_time, phase)
VALUES ($escaped_story_name, $escaped_story_text, $escaped_story_point, $escaped_uploader_name, $escaped_story_date, $escaped_story_time, $escaped_phase)";
$insertResult = mysqli_query($db, $insertStoryData)
or die("insert story failed" . mysqli_error($db));
What format the date and time are
21/9/2013 - Date
19:21:20 - Time
Story date and Story time SQL fields
story_date is of type date and story_time is of type time
The time and date are produced by the JavaScript before an AJAX request to the PHP script.
Date on mysql follow the format: yyyy-mm-dd
Time on mysql follow the format: hh:ii:ss
Hope it helps.
You need to enclose the date and the time in single quotes (separately of course). Also, your date format is incorrect - it needs to be yyyy-mm-dd.

Select query with month and year condition

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.

Trying to find a record between two dates

My Queries looks like this
WHERE `Project`.`user_id` = 9
AND `Project`.`project_status` = 1
AND `Project`.`project_type_id` = 4
AND `Project`.`approval_date` >= '01/08/2012'
AND `Project`.`approval_date` <= '01/12/2012'
I do have a record that matches this criteria, I actually created the query based on one of the records but it does not return. I really do not want to use the BETWEEN because the application can either use start date or end date
You should convert '01/08/2012' and '01/12/2012' to MySQL date/time values using STR_TO_DATE()
e.g.
STR_TO_DATE('01/08/2012', '%d/%m/%Y')
Well that's really only part of the query -- any, "between" works differently than you imply - here's an excerpt from the MySQL manual:
•expr BETWEEN min AND max If expr is greater than or equal to min and
expr is less than or equal to max, BETWEEN returns 1, otherwise it
returns 0. This is equivalent to the expression (min <= expr AND expr
<= max) if all the arguments are of the same type. Otherwise type
conversion takes place according to the rules described in Section
11.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments.
All that aside what is the datatype of the "approval_date" column?
This looks as if it should work on first glance, however the values you have supplied are not valid datetimes. Try this instead
WHERE `Project`.`user_id` = 9
AND `Project`.`project_status` = 1
AND `Project`.`project_type_id` = 4
AND `Project`.`approval_date` >= '2012-01-08 00:00:00'
AND `Project`.`approval_date` <= '2012-01-12 00:00:00'
you can still use BETWEEN
WHERE `Project`.`user_id` = 9
AND `Project`.`project_status` = 1
AND `Project`.`project_type_id` = 4
AND (`Project`.`approval_date` BETWEEN '2012-01-08 00:00:00' AND '2012-01-12 23:59:59')
My main problem was getting the string to format into datetime. kissmyface was correct on just structuring it correctly so mysql could understand. So I used php to convert it before sending it out.
"Project.approval_date >= '".date("Y-m-d H:i:s", strtotime($start))."'";
"Project.approval_date <= '".date("Y-m-d H:i:s",strtotime($end))."'";
this was pretty much my solution. unless anyone knows away to do the exact same thing on mysql side.

Categories