add seconds to a date using php? - php

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

Related

Calculate the hours minutes and days of difference between two date and hours

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

Convert day hour-minute just to hour-minute

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.

how to get a time difference between two dates in ("Y-m-d H:i:s") format?

I'm trying to get the difference between two date-times and return it as a minute. Date & Time are taken in date("Y-m-d H:i:s") format. But it seem i can't get it right. I did it
$time=date("Y-m-d H:i:s");
$time=date("2014-01-13 08:18:25");
$interval = $time->diff($login_time);
$elapsed = $interval->format(%i minutes);
echo $elapsed;
And This is showing a massage
"Call to a member function diff() on a non-object"
As I am not good enough with date formatting. So Please help me.
What is the way to go about this?
Try this:
$date1 = new DateTime('2013-01-13 04:10:58');
$datediff = $date1->diff(new DateTime('2013-09-11 10:25:00'));
echo $datediff->i;
For more details see this link : http://www.php.net/manual/en/book.datetime.php
Get difference is minutes between two dates:
$now = new DateTime;
$before = new DateTime('2014-01-10 08:18:25');
$diff = $now->diff($before);
$elapsed = $diff->days * 24 * 60 + $diff->h * 60 + $diff->i;
demo
Use the below code for getting time in all expected parameter.
$time1=date("Y-m-d H:i:s");
$time2=date("2014-01-13 08:18:25");
$seconds = strtotime($time1) - strtotime($start);
$year = floor(($seconds)/(60*60*24*365)); $months = floor($seconds /
86400 / 30 ); $week = floor($seconds / 604800); $days =
floor($seconds / 86400); $hours = floor(($seconds - ($days * 86400))
/ 3600); $minutes = floor(($seconds - ($days * 86400) - ($hours *
3600))/60); $seconds = floor(($seconds - ($days * 86400) - ($hours *
3600) - ($minutes*60)));

how to calculate hours,minutes and seconds from total minutes in php?

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

Does not get the time difference correct in PHP

$time1 = "01:00";
$time2 = "04:55";
list($hours1, $minutes1) = explode(':', $time1);
$startTimestamp = mktime($hours1, $minutes1);
list($hours2, $minutes2) = explode(':', $time2);
$endTimestamp = mktime($hours2, $minutes2);
$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));
echo $hours.':'.$minutes;
exit;
Outputs 4:55, should be 3:55 ?
Whats wrong here? If it is 01:00 and 02:00, it works fine, but not with the above?
Use floor instead of round...
Or just cast to integer.
$hours = (int) ($seconds / (60 * 60));
Too many calculations when PHP can do it for you with also reducing possibility of error
$time1 = Datetime::createFromFormat("h:i", "01:00");
$time2 = Datetime::createFromFormat("h:i", "04:55");
$diff = $time1->diff($time2);
var_dump($diff->format("%h %i"));
Output
string '3:55' (length=4)
You can also save yourself some time by using strtotime:
$time1 = strtotime("01:00");
$time2 = strtotime("04:55");
$seconds = $time2-$time1;
$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / (60 * 60));
echo $hours.':'.$minutes;
As mentioned, using floor will produce the result you need:
Result
3:55

Categories