Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello everyone,

it is to calculate the number of minutes between two dates from a table in a MySQL database
my incident table (.., date_creation, hour_creation, date_closing, hour_closing,) I want to know the number of minutes between the date of creating and closing , taking into hours account also.

I need a mysql function OR a PHP script to do this?
thank you in advance
Posted

 
Share this answer
 
Comments
DamithSL 3-Jul-14 22:22pm    
some one down voted may be because OP saving date and time in different columns and OP can't direct use DATEDIFF function.
But this is way to go, OP should change the database and use this function. I have added my five cents in my Answer[^], 5wd!
I would suggest you to look at
TIMEDIFF[^] function.

PHP way: you need to use mktime functions:
example would be:
PHP
<?php
$time1="2014-04-15 10:10:12";
$time2="2014-04-15 12:10:12";
$time1_int=strtotime ($time1);
$time2_int=strtotime ($time2);
$diff=$time2_int-$time1_int;

$sec=$diff%60;
$minitue=floor($diff/60)%60;
$hour=floor(floor($diff/60)/60);
echo $hour.":".$minitue.":".$sec;

There might be far better solution for this.
 
Share this answer
 
I would not save date and time separately, Best practice is use DATETIME column type and save creation and closing date and time. you can use DATEDIFF[^] function and get the total minutes as you need. Otherwise you need to do lot of calculation and effort to combine date and time values separately saved in database and do the calculation. Not that when you need to display Date or Time separately in UI or report you can do it on UI layer. That will help you to even format the dates and times based on client culture.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900