I have two times and I want to subtract them by using a PHP built-in function or Carbon. For example, I have following times:
00:05:10, i.e. 5 Minutes 10 Seconds
00:03:10, i.e. 3 Minutes 10 Seconds
If I subtract them, the total time would be 00:08:20. Can someone kindly guide me how I can make such a subtraction?
Convert to timestamp i think, after compare and substract
If you convert both times to unix timestamps, take away a "base timestamp" (For 00:00:00) from each, and then add them together you will get the number of seconds value for the 2 timestamps. Through some simple operations we can then get the total number of hours, minutes, and remaining seconds, then format them in your input style.
function add_times($a, $b) {
$base = strtotime('00:00:00');
$seconds = (strtotime($a) - $base) + (strtotime($b) - $base);
$hours = floor($seconds / 3600);
$minutes = floor($seconds / 60) % 60;
$seconds = $seconds % 60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
echo add_times('00:05:10', '00:03:10');
Related
i try to make time difference with carbon
$dt = Carbon::parse('2018-07-15 00:00:00');
$now = Carbon::now('Asia/Dubai'); //set current time
$seconds = $now->diffInSeconds( $dt ); //difference turn into second
$days = $dt->diffInDays($dt->copy()->addSeconds($seconds));
$hours = $dt->diffInHours($dt->copy()->addSeconds($seconds)->subDays($days));
$minutes = $dt->diffInMinutes($dt->copy()->addSeconds($seconds)->subHours($hours));
$days result are 12 (its right).
$hours result are 8 (seems not right).
$minutes result are 17299 (clearly wrong).
how to get the result for example 12 day 5 hours 45 minutes
Actually functions like diffInSeconds give total difference in seconds that's why the number is so large,to get the minutes for the time difference right you can use -:
$minutes = ($now->minute - $dt->minute);
I have an employee timesheet where durations are divided by the number of days an employee works.
E.g 25:00 over 3 days = 25:00/3=08:20
I have tried the simple divide query above, however this does not show a result. Is it possible to divide a h:m string?
Best approach would be to convert to seconds and use date to display it.
$time ="25:00";
$days = 3;
list($hours, $minutes) = explode(":", $time);
$minutes += $hours*60;
$seconds = $minutes*60;
date_default_timezone_set ("UTC");
echo "new time: " . date("h:i", $seconds/$days);
See the result here
I am using gmdate in PHP to convert seconds to H:i:s
I have this code:
echo gmdate("H:i:s", '480002');
So i should be converting 480002 seconds to H:i:s which should show
133:20:02
but its only showing
13:20:02
Use this
<?php
$init = 480002;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;
echo "$hours:$minutes:$seconds";
?>
gmdate is working with date values not dateinterval values, H in the format string therefore represents the number of hours since midnight. Generally days don't have 133 hours, so this is interpreted as 5 days and 13 hours. You haven't asked it to display the days part, so it just shows 13 hours.
There are various ways to calculate the total number of hours.
I'd suggest looking at this question:
Calculate number of hours between 2 dates in PHP
I'm trying to create a button or link that will expire after 1 hour.
I'm setting the time the visitor hit the page with a cookie.
Most of the code examples I have seen only give the time that has passed and not the time left.
example: Link will expire in 0 hours, 30, mins and 34 seconds
This is just some rough code :
//Setting cookie example
setcookie('previous_time', time(), time()+3600*1);
$current_time = time();
$previous_time = $_COOKIE['previous_time'];
$time_diff = $current_time-$previous_time;
This is where I'm stuck, I have no idea how to convert the $time_diff timestamp
into a format like "expire in 0 hours, 30, mins and 34 seconds"
Many thanks.
To format your time difference, just do some math, since your $time_diff is just the number of seconds between the two times:
$hours = floor( $time_diff / 3600);
$minutes = floor( ($time_diff / 60) % 60);
$seconds = $time_diff % 60;
echo "$hours hours, $minutes minutes, $seconds seconds\n";
So, a value of 20712 would produce :
5 hours, 45 minutes, 12 seconds
Using your formula comparing timestamps, the difference is in seconds.
So, $time_diff / 60 gets you minutes; divide by another 60 to get hours; etc.
I agree with nickb that cookie based is tamper-able, but saying you mark the first visit somehow for an hour ahead when the link will expire:
// set when we are counting down to
setcookie('expires_at', time()+3600, time()+3600);
// we are counting down not up (for "expires in" not "valid since" logic)
$time_diff = $_COOKIE['expires_at'] - time();
$minutes = floor($time_diff / 60);
$seconds = floor($time_diff % 60);
// zero hours since the link will only be valid for one hour max
echo sprintf('expire in 0 hours, %d mins and %d seconds', $minutes, $seconds);
Then you can do:
if($time_diff > 0){
echo '...';
}
how I can sum two dates?
I have two date in this format j h:i:s and I will like to sum them
I found this script but no reference about days and I'm new to this, here is the script, maybe somebody can make the necessary changes.
<?php
/**
* #author Masino Sinaga, http://www.openscriptsolution.com
* #copyright October 13, 2009
*/
echo sum_the_time('01:45:22', '17:27:03'); // this will give you a result: 19:12:25
function sum_the_time($time1, $time2) {
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
return "{$hours}:{$minutes}:{$seconds}";
}
?>
Usage:
All that I need is to sum two dates from two task like:
Task 1 will take 1 day 10 hour 20 mins 10 sec
Task 2 will take 3 days 17 hours 35 mins 40 sec
so the result will be the total time between task 1 and two
just convert the dates using strtotime and sum the timestamps, than convert to the date again using date function:
$tmstamp = strtotime($date1) + strtotime($date2);
echo date("Y m d", $tmstamp) ;
but why would you add two dates in the first place?
BAsically what they do, is convert hours and minutes into seconds and add those. Then they convert back to hours/minutes.
You simply have to convert days to seconds too:
$seconds += $days * 3600 * 24
and then convert back:
$days = floor($seconds/(3600*24));
$seconds -= $days*3600*24