divide PHP time (h:i) - php

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

Related

How to substract two times in php using carbon?

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

Carbon show time different days hours minutes second

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

Calculating countdown between now and x amount of days ahead to cetain time of day

I am completely stuck here. I am trying to get how many days hours and minutes to echo from a calculation from the current time to 7 days from now at 6pm. I look at the amount of seconds produced from my $difference variable and when I do the math to convert it to days hours and minutes it is correct but for some reason when I call the specific days, hours, and minutes in my output statement it is incorrect. What am I doing wrong. Here is the code.
<?php
date_default_timezone_set('America/New_York');
$nextWeek = strtotime('+7 days');
$m = date('n', $nextWeek);
$d = date('j', $nextWeek);
$y = date('Y', $nextWeek);
$difference = mktime(18,0,0,$m,$d,$y) - time();
echo '<p>Current date and time is' .date(' l F d, Y '). 'at '.date('g:i a').' You have an appointment in a week on '.date(' n/j/Y ', $nextWeek).' at 6pm. There are ' .date(' j ', $difference).' days, ' .date(' g ', $difference).' hours, and ' .date(' i ', $difference).' minutes until your appointment.</p>';
echo mktime(18,0,0,$m,$d,$y),"\n";
echo $difference;
?>
The problem is that you're using PHP's date() function on a number that doesn't represent a date. Your variable $difference represents the difference between the two dates, in seconds. To get the right output, you should write your own function to convert these seconds to number of days, hours, minutes, etc.
It might look something like this:
function getTimeText($seconds)
{
$return = array();
$return["days"] = floor($seconds/86400); // 86400 seconds in a day
$seconds -= ($return["days"]*86400);
$return["hours"] = floor($seconds/3600); // 3600 seconds in an hour
$seconds -= ($return["hours"]*3600);
$return["minutes"] = floor($seconds/60); // 60 seconds in a minute
return $return;
}
Try checking out this. The example part way down the page shows you how to find the difference between two dates in days. You should be able to use it to return the difference between the current time and the time in 7 days at 18:00 by changing the format.

Sum of two dates in PHP

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

Php get how many days and hours left from a date

I have a created_at date saved liked this "2011-09-23 19:10:18" And I want to get the days and hours left
until the date is reached. How do I do that? and column name in database remain days automatically update daily with remain days, please solve this
PHP fragment:
<?php
//Convert to date
$datestr="2011-09-23 19:10:18";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)
//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));
//Report
echo "$days days $hours hours remain<br />";
?>
Note the hour-round and no minutes/seconds consideration means it can be slightly inaccurate.
This should seed your endeavor.
getdate(strtotime("2011-09-23 19:10:18"))
Full conversion:
$seconds = strtotime("2011-09-23 19:10:18") - time();
$days = floor($seconds / 86400);
$seconds %= 86400;
$hours = floor($seconds / 3600);
$seconds %= 3600;
$minutes = floor($seconds / 60);
$seconds %= 60;
echo "$days days and $hours hours and $minutes minutes and $seconds seconds";
as of PHP 5.3.0 you could use build-in Date object:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
http://www.php.net/manual/en/book.datetime.php
it would be something like
echo $date = date("Y-m-d H:i:s");echo "\n";
$original=time($date);
$modified = "2011-09-23 19:10:18";
echo date("Y-m-d H:i:s",$modified);echo "\n";
The easiest way is improved answer from CountZero. I used this solution for counter for time remaining before expiration of offer. Addition to first three lines of CountZero code:
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
And now, you can use string functions to moderate your return values, to merge all or add '0' or '00' in front of the values.
You can find current date and time using date() function and then subtract
$x = 2011-09-23 - current_date
this will give you no. of days left.
Do same with time as well ..
Hope this helps

Categories