Sum of two dates in PHP - 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

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

divide PHP time (h:i)

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

User friendly countdown to midnight

I have a cron job that runs at midnight which resets all user limits for that day. I want to display something along the lines of Your limits reset in 1 hour 14 minutes to my users. Basically a countdown until midnight (server time).
At the moment I'm using this to find midnight:
strtotime('tomorrow 00:00:00');
which returns a timestamp for when midnight rolls over, but I have no idea how to display a user friendly countdown. Is there a PHP library for this or is this pretty easy without a library?
Simply this gives you left-minutes;
$x = time();
$y = strtotime('tomorrow 00:00:00');
$result = floor(($y - $x) / 60);
But you need to filter $result;
if ($result < 60) {
printf("Your limits rest in %d minutes", $result % 60);
} else if ($result >= 60) {
printf("Your limits rest in %d hours %d minutes", floor($result / 60), $result % 60);
}
Since you're looking for a rough estimate, you could leave out the seconds.
$seconds = strtotime('tomorrow 00:00:00') - now();
$hours = $seconds % 3600;
$seconds = $seconds - $hours * 3600;
$minutes = $seconds % 60;
$seconds = $seconds - $minutes *60;
echo "Your limit will reset in $hours hours, $minutes minutes, $seconds seconds.";
It is quite easy, just a little mathematics along with finding the difference in seconds between then and now.
// find the difference in seconds between then and now
$seconds = strtotime('tomorrow 00:00:00') - time();
$hours = floor($seconds / 60 / 60); // calculate number of hours
$minutes = floor($seconds / 60) % 60; // and how many minutes is that?
echo "Your limits rest in $hours hours $minutes minutes";

Want to subtract hour to hour, minutes to minutes, and seconds to seconds from between two dates

<?php
$ts='2011-04-13 23:00:00';
$ts1='2011-04-14 15:45:00';
echo $addtime = date("h:i:s", mktime(date("h", $ts1)- date("h", $ts),date("i", $ts1)- date("i", $ts),date("s", $ts1)- date("s", $ts),0,0,0));
?>
It gives a result but it is not correct in many cases. How do I fix it?
Your expected result would be 16:45:00 for the given example, right? So you want the difference between the two given dates in hours:minutes:seconds.
<?php
//initial strings
$ts='2011-04-13 23:00:00';
$ts1='2011-04-14 15:45:00';
//converting to time
$start = strtotime($ts);
$end = strtotime($ts1);
//calculating the difference
$difference = $end - $start;
//calculating hours, minutes and seconds (as floating point values)
$hours = $difference / 3600; //one hour has 3600 seconds
$minutes = ($hours - floor($hours)) * 60;
$seconds = ($minutes - floor($minutes)) * 60;
//formatting hours, minutes and seconds
$final_hours = floor($hours);
$final_minutes = floor($minutes);
$final_seconds = floor($seconds);
//output
echo $final_hours . ":" . $final_minutes . ":" . $final_seconds;
?>
This gives me correct results. Hope I got your problem right!

Get duration between date time formats

how to get duration between start_date and end_date in hrs min sec format in php?
$start_date=2012-03-23 11:58:14 and $end_date=2012-03-24 11:54:29
Use DateTime class:
$start_date = new DateTime('2012-03-23 11:58:14');
$end_date = new DateTime('2012-03-24 11:54:29');
$dd = date_diff($end_date, $start_date);
To get hours use $dd->h, minutes - $dd->i, seconds - $dd->s.
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
I would
$start_time = strtotime($start_date);
$end_time = strtotime($end_date);
$difference = $end_time - $start_time;
echo date('H:i:s', $difference);
EDIT
I made an error in assuming that the time difference would be less then a day, so if the time difference is greater then a day, you will only see the Hours:minuetes:seconds, which is probably not what you want (if it is ignore this)
So I would now
$seconds = $difference % 60; //seconds
$difference = floor($difference / 60);
$min = $difference % 60; // min
$difference = floor($difference / 60);
$hours = $difference; //hours
echo "$hours : $min : $seconds";
Sorry for the correction
This function will help you
function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
/*
$interval can be:
yyyy - Number of full years
q - Number of full quarters
m - Number of full months
y - Difference between day numbers
(eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
d - Number of full days
w - Number of full weekdays
ww - Number of full weeks
h - Number of full hours
n - Number of full minutes
s - Number of full seconds (default)
*/
http://php.net/manual/en/function.date-diff.php
format those string to date
transform those date to milliseconds
do endate - stardate
from the result calculate the h:mm:ss
See it working here: http://codepad.viper-7.com/FPuOks
$start_date="2012-03-22 11:58:14";
$end_date="2012-03-24 11:54:29";
$start_time = strtotime($start_date);
$end_time = strtotime($end_date);
$difference = $end_time - $start_time;
echo sprintf("%02d%s%02d%s%02d", floor($difference/3600), ':', ($difference/60)%60, ':', $difference%60); // outputs 47:56:15

Categories