I am developing one website in php.
I need to add jobs to the sql server 2008 database.
I am taking job duration in minutes and i want to convert it in to H:i:s format.
I am using this code :
$hours = (int)($duration / 60);
$minutes = $duration - ($hours * 60);
$time = $hours.":".$minutes;
It works fine with int duration 30,60,100,120 etc.
but if duration is 1.33 or 5.40 than the output should be 00:01:30 or 00:05:40 respectively.
How can i do this ?
try this
<?php
$duration = "1.33";
$hours = (int)($duration / 60);
$minutes = $duration - ($hours * 60);
date_default_timezone_set('UTC');
$date = new DateTime($hours.":".$minutes);
echo $date->format('H:i:s');
?>
output
00:01:33
Use the DateInterval class if you're running PHP > 5.3.0:
$interval = new DateInterval('100 minutes');
$interval->format('%H:%I:%S'); // prep for MySQL database.
As you are taking minutes, this code is working perfectly
$valueInMinutes = 135;
echo intval($valueInMinutes / 60);
echo " h ";
echo intval($valueInMinutes % 60);
and will output
2 h 15
If you have to manage the days, just change a little bit the code to:
$value = 1505;
echo intval($value / 1440);
echo " day(s) ";
echo intval(($value % 1440) / 60);
echo " h ";
echo intval($value % 60);
will output
1 day(s) 1 h 5
You can do with php date function :
$hours = (int)($duration / 60);
$minutes = $duration - ($hours * 60);
$time = date("H:i:s", strtotime($hours.":".$minutes));
echo $time;
More info on date can be found here http://php.net/manual/en/function.date.php
Hope this helps :)
Try this way
$time = 5.40;
$newtime = floatval($time);
echo number_format($newtime,2,':',null);
Try this
echo $time = date("H:i:s", strtotime($hours)) . ":" . date("H:i:s", strtotime($minutes));
Output
01:33:00 : 05:40:00
Related
I have a PHP and MySQL code that should calculate the hours minutes and days of difference between two date and hours. It works well, just adding 20 hours and 20 minutes more than normal. And I remove the DATE part and put the date and time manually, it works fine.
I don't understand what happens.
$fecha = $row["fecha"];
$data= $row["hora"];
$start = strtotime("$fecha $hora");
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));
$end = strtotime("$currentDate");
$totaltime = ($end - $start) ;
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == Start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}
Edit
start 2019-12-29 21:27:50 . end 2019-12-31 0:51:50 = 47:51:16
As you can see it calculates badly.
A simple example like this would do the job :
$mydatetime = new DateTime();
$datefromdb = new DateTime('2018-03-05 10:10:00');
$interval = $mydatetime->diff($datefromdb);
$date_count = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $date_count;
This is your code it should work
$fecha = $row["fecha"];
$data= $row["hora"];
$start = strtotime("$fecha $data");
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));
$end = strtotime("$currentDate");
$totaltime = ($end - $start) ;
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == $start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}
problem was in the string.
$data= $row["hora"];
and I use this
$start = strtotime("$fecha $hora");
And don't take the hours and calculate only for days.
Thank you
I have this code
echo $workedtime->format('d H:i'); //shows 01 10:01
I want it to show 34:01!
How can I do it?
You'll need two variables for this, where one of them is for when you started working and the second one is for when you finished work.
This could easily be solved by getting the date in UNIX timestamp for each variables.
function timeAgo($startTime, $endTime){
$seconds = strtotime($endTime) - strtotime($startTime);
$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / 60 / 60);
return $hours . ":" . $minutes;
}
$startTime = "2018-12-24 09:00:00";
$endTime = "2018-12-25 15:30:00";
echo timeAgo($startTime, $endTime);
This returns 30:30
If you're using DateTime, you can just output the DateTime as UNIX timestamp instead of using strtotime. (Assuming you pass the DateTime as arguments for the function).
function timeAgo($startTime, $endTime){
$seconds = $endTime->getTimestamp() - $startTime->getTimestamp();
$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / 60 / 60);
return $hours . ":" . $minutes;
}
echo timeAgo($startTime, $endTime); // $startTime and $endTime has to be DateTime.
I wanted to convert this into hours:
<?php
( strtotime('12:45:00') - strtotime('11:00:00') ) / 60 (This is for minutes)
?>
You can convert minutes to hour using following function:
<?php
function convertToHoursMins($time, $format = '%02d:%02d') {
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
echo convertToHoursMins(250, '%02d hours %02d minutes');
For more info check this post
Although I'm not sure whether I understand you right, here's an example:
<?php
$resultM = ( strtotime('12:45:00') - strtotime('11:00:00') ) / 60; // (This is for minutes)
$resultH = ( strtotime('12:45:00') - strtotime('11:00:00') ) / 3600; // (This is for hours)
$resultH1 = floor(( strtotime('12:45:00') - strtotime('11:00:00') ) / 3600); // (This is for hours)
echo "Minutes=$resultM<br />Hours(float)=$resultH<br />Houres(floor)=$resultH1<br />";
?>
Try this example. Positive and Negative answer will depends on date provided first in date_diff function.
$date1 = date_create("12:45:00");
$date2 = date_create("11:00:00");
$diff=date_diff($date1,$date2);
echo "Difference in Hours\n";
echo $diff->format("%R%H hours")."\n";
echo "Difference in Minutes\n";
echo $diff->format("%R%i minutes");
click here for Working Demo
Another Example
$date2 = new DateTime("12:45:00");
$date1 = new DateTime("11:00:00");
echo "\n\n".$date2->diff($date1)->format("%H hours %i minutes");
Hi I have a two variable in 24 hour time format and want to compute the number of hours worked. But I get negative and wrong value
I'm using PHP and here's my code
$endtime = date( 'g:i A', strtotime( $itInfo['endTime'] ) );
$startTime = date( 'g:i A', strtotime( $itInfo['startTime'] ) );
$timeDiff = (strtotime($endtime) - strtotime($startTime))/60/60;
$total = strtotime($endtime) - strtotime($startTime);
$hours = floor($total / 60 / 60);
$minutes = round(($total - ($hours * 60 * 60)) / 60);
echo "FROM ".$itInfo['startTime']." TO ".$itInfo['endTime']." (".$hours.'.'.$minutes."hours)";`
Here's the output FROM 22:00 TO 03:00 (-19.0hours) which is wrong the output should be 5 Hours.
Try this:
$timeDiff = strtotime($itInfo['endTime']) - strtotime($itInfo['startTime']);
echo substr('00'.($timeDiff / 3600 % 24),-2)
.':'. substr('00'.($timeDiff / 60 % 60),-2)
.':'. substr('00'.($timeDiff % 60),-2);
As others have stated, best to work with timestamps. But with your current code, you should be able to add this right before the echo:
if ($itInfo['startTime'] > $itInfo['endTime']) {
$hours = 24 - $hours;
}
This would be: 24 - 19 = 5.
Also, be sure to take add abs() in your $total variable as well:
$total = abs(strtotime($endtime) - strtotime($startTime));
I wanted to find the End Time of a incoming visitor . I have recorded the Intime & duration in My DB through an API , But the API does not provide the option for End Time. It looks like i have to calculate the End time, from ( Intime & Duration ) . However i did find the end time. But it is taking too much time to process the code and i am geting error like Fatal error: Maximum execution time of 600 seconds exceeded in D:\xampp-portable\htdocs\logmein\ on line 68 .
Please look into my code and tellme a way to customize my Code in a very easier way.
Start time-11/19/2014 4:57 AM,Duration-1423
I want :Start time:2014-11-20 12:48:17,
End Time:2014-11-20 13:07:17,
Duration-00:19:35
<?php
$WaitingTime=1423;
$i = ($WaitingTime / 60) % 60;
$duration = sprintf('%02d:%02d:%02d', ($WaitingTime / 3600), ($WaitingTime / 60 % 60), $WaitingTime % 60);
$timestamp = strtotime($strat_time);
$chat_start=date("Y-m-d H:i:s", $timestamp);
$time = new DateTime($chat_start);
$time->add(new DateInterval('PT' . $i . 'M'));
$chat_end = $time->format('Y-m-d H:i:s');
?>
Something like the following achieves this:
<?php
$durationSeconds = 1423;
$startDateString = '11/19/2014 4:57 AM';
$startTime = new DateTime($startDateString);
$startString = $startTime->format('Y-m-d H:i:s');
$duration = new DateInterval('PT'.$durationSeconds.'S');
$hours = floor($durationSeconds / 3600);
$minutes = floor(($durationSeconds - ($hours * 3600)) / 60);
$seconds = $durationSeconds % 60;
$durationString = $hours.':'.$minutes.':'.$seconds;
$startTime->add($duration);
$endString = $startTime->format('Y-m-d H:i:s');
Thanks all,This is my answer.
<?php
$WaitingTime=1423;
$strat_time="11/19/2014 2:49:23";
$dateinsec=strtotime($strat_time);
$newdate=$dateinsec+$WaitingTime;
$chat_start=date("Y-m-d H:i:s", $dateinsec);
$chat_end= date('Y-m-d H:i:s',$newdate);
$duration = sprintf('%02d:%02d:%02d', ($WaitingTime / 3600), ($WaitingTime / 60 % 60), $WaitingTime % 60);
<?
This is my answer2
$time1 = strtotime("$call_start");
$time2 = strtotime($call_end);
$diff = $time2 - $time1;
$ath = sprintf('%02d:%02d:%02d', ($diff / 3600), ($diff / 60 % 60), $diff % 60);