Jump to content

[SOLVED] MySQL date math


simon551

Recommended Posts

 

should be in mySQL forum sorry. if you know how to move then please do.

 

Hi,

 

I have this query to sum timesheet entries in my application. The user can enter hours and/or minutes and then we calculate the value. My question is about whether I can change the value back to hours:minutes for printing.

 

This is my query right now:

SELECT timesheet_entries.id, Sum(COALESCE(timesheet_entries.hours,0))  + Sum(COALESCE(timesheet_entries.minutes,0)/60) AS Rtotal FROM timesheet_entries  GROUP BY timesheet_entries.id

 

It takes the hours and the minutes and combines them to output a number like 3.5 for 3 hours 30 minutes. Is there a way to break that number back down and convert it to at least look like 3:30. Some users, in testing, are getting confused by the output of 3.5 after entering 3 hours and 30 minutes (which is annoying but I digress.)

Link to comment
https://forums.phpfreaks.com/topic/61394-solved-mysql-date-math/
Share on other sites

In SQL:

mysql> SELECT CONCAT(FLOOR(3.5),' hours, ',(3.5 * 60) % 60, ' minutes');                               
+-----------------------------------------------------------+
| CONCAT(FLOOR(3.5),' hours, ',(3.5 * 60) % 60, ' minutes') |
+-----------------------------------------------------------+
| 3 hours, 30.0 minutes                                     | 
+-----------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT CONCAT(FLOOR(3.5),':',LPAD(TRUNCATE((3.5 * 60) % 60,0),2,'0'));                                     
+----------------------------------------------------+
| CONCAT(FLOOR(3.5),':',TRUNCATE((3.5 * 60) % 60,0)) |
+----------------------------------------------------+
| 3:30                                               | 
+----------------------------------------------------+
1 row in set (0.01 sec)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.