How can I subtract 2 dates from one another? - php

I want to subtract a date in the future from todays date, I want it to display both the day, hour and minutes until the target date.
Here is my code
<?php
date_default_timezone_set('Europe/London');
$format = "h:i d";
$date = date($format);
$target = date($format, mktime(0,0,0,12,8,2011));
echo date($format, $target-$date);
?>
Kind regards,
Adam

Don't use date for operations, it's meant for displaying dates. Instead subtract 2 timestamps:
...
$date = mktime(now...);
$target = mktime(0,0,0,12,8,2011);
echo date($format, $target - $date);
But you have to realize timestamps start in 1970 and end in 2038, therefore, for example, 2011 - 2007 = 1974.
More suitable in your case would be date_diff as #Kerrek SB suggested in comment.
Example (from php.net):
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days'); // +2 days

Maybe this will help you
<?php
$target = mktime (23, 34, 0, 9, 28, 2009);
$today = mktime();
$difference =($target-$today) ;
$days = $difference / 3600 / 24;
$difference = $difference - floor($days) * 3600 * 24;
$hours = $difference / 3600;
$difference = $difference - floor($hours) * 3600;
$minutes = $difference / 60;
echo "Days: ";
echo floor($days);
echo "<br />";
echo "Hours: ";
echo floor($hours);
echo "<br />";
echo "Minutes: ";
echo floor($minutes);
?>

Related

How i can Check That a user cannot enter time hours more than 8 hours in php

My code is taking for 8 hours from time and date field but when i give both for example i set 9 am to 4 pm then its OK. but when i insert 9am to 6am it also allow that one its almost 21 hours difference so how can i do it any one please?
<?php
elseif (isset($_POST['from']) && ($_POST['to']))
{
$from_time = $_POST['from'];
$to_time = $_POST['to'];
$starttimestamp = strtotime($from_time);
$endtimestamp = strtotime($to_time);
$difference = abs($endtimestamp - $starttimestamp) / 3600;
if ($difference > 8){
echo "<script>alert('Oops!Your Daily Availablity Is More Than 8 Hours')</script>";
}else{
$query = "update donor SET from_time='" . $from_time . "',to_time='" . $to_time . "' where id=" . $_SESSION['id'];
}
you have to specify full date time in request.
<?php
$date = date("Y-m-d h:i:s", strtotime($row['time_d']))
?>
<input type="time" class="form-control" values="<?= $date ?>" id="until_t" name="from"/>
Then you will got correct difference between both start and end time.
Take the input in 24 hrs format including DataTime. Then change your code as below.
I hope this code will solve your problem.
<?php
date_default_timezone_set('Asia/Kathmandu');
$datetime1 = new DateTime('2010-10-11 24:00:00'); // change date & time
$datetime2 = new DateTime('2010-11-11 24:00:00'); //change date & time
$interval = $datetime1->diff($datetime2);
echo $interval->format('%Y-%m-%d %H:%i:%s')."<br />";
$year = $interval->format('%Y') * 365 * 24;
$month = $interval->format('%m') * 30 * 24 ;
$day = $interval->format('%d') * 24 ;
$hour = $interval->format('%H');
$minute = $interval->format('%i')/ 60;
$seconds = $interval->format('%s')/(60*60);
$time_diff = $year + $month + $day + $hour + $minute + $seconds;
echo $time_diff;
if ($time_diff > 8) {
echo 'More then 8 hour';
}else{
echo 'ok';
}
?>

Php Date Conversion and date difference

I have two dates like
2016-01-25 07:33:54 and current date 30-01-2016 01.27.00
I want to get difference between these dates with dates in number of hours, number of minutes and number of seconds .
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$difference = strtotime($toDate)-strtotime($fromDate);
echo "Seconds : ".$difference."<br>";
echo "Minutes : ".($difference/60)."<br>";
echo "Hours : ".($difference/(60*60));
Or
A Easy way
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$datetime1 = new DateTime($toDate);
$datetime2 = new DateTime($fromDate);
$interval = $datetime1->diff($datetime2);
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
echo "$days days, $hours Hrs, $minutes Mins, $seconds Sec";
http://php.net/manual/en/function.strtotime.php
use strtotime to find time difference between two dates.
http://php.net/manual/en/class.datetime.php
You can also try this.
checkout my code below.
<?php
$datetime1 = new DateTime('2014-02-16 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days')." days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes".$interval->format('%s')." Seconds";
?>

How can I tell what the date_diff duration is?

I have the following code:
$sStart = date('Y-m-d H:i:s', strtotime($row['sStart']));
$sEnd = date('Y-m-d H:i:s', strtotime($row['sEnd']));
$sStartDate = new DateTime($sStart);
$sEndDate = new DateTime($sEnd);
$diff = date_diff($sStartDate, $sEndDate, true);
$dur = $diff->format('%h:%i');
$row['sStart'] and $row['sEnd'] are datetimes from a MySQL database.
How can I tell if $diff is over 24 hours long?
The $diff is of type DateInterval.
I think that this will give you the number of hours using the result of the date_diff:
$hours = ($diff->days * 24) + $diff->h;
if ($hours > 24) {
// etc ..
}
Since 24 hours is 1 day, you can use the %a modifier to the DateInterval::format call to calculate the number of days the interval crosses:
$sStartDate = DateTime::createFromFormat('Y-m-d H:i:s', '2013-05-13 21:56:00');
$sEndDate = DateTime::createFromFormat('Y-m-d H:i:s', '2013-05-14 21:56:01');
$diff = date_diff($sStartDate, $sEndDate, true);
$days = $diff->format('%a');
if (1 <= $days) {
echo '24 hours or more.';
} else {
echo 'less than 24 hours.';
}
See it live.

how can I calculate two times between two fixed times?

I want to calculate the hours in the attendance sheet
but I don't want to calculate the hours and minutes that are not between these two times that are: 08:00 to 14:00
for example:
employee A arrived to work at 09:00 and left at 16:00
the result should be 5 hours
and if Employee B arrived to work at 8:30 and left at 14:00
the result should be 5hours and 30 mins
kind regards
here is your answer
<?php
$restriction_start = strtotime("30-Mar-2015 08:00:00");
$restriction_end = strtotime("30-Mar-2015 14:00:00");
$start_time = strtotime("30-Mar-2015 09:00:00");
$end_time = strtotime("30-Mar-2015 16:00:00");
echo calculateTime($start_time, $end_time, $restriction_start, $restriction_end);
function calculateTime($start_time, $end_time, $restriction_start, $restriction_end)
{
$start = $restriction_start;
if($start_time>$restriction_start)
{
$start = $start_time;
}
$end = $restriction_end;
if($end_time<$restriction_end)
{
$end = $end_time;
}
$time_diff = $end-$start;
$output = gmdate("H:i:s", $time_diff);
return $output;
}
?>
You can use Datetime object to do that. There is the documentation : http://php.net/manual/en/datetime.diff.php
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
var $NbrSec = mktime($hour1, $minute1, $second1, $month1, $day1, $year1)
- mktime($hour2, $minute2, $second2, $month2, $day2, $year2);
var $Hr = ((int)($NbrSec / 60)) * 60;
var $Mn = $NbrSec % 60;
echo $Hr . ":" . $Mn;

time difference in HH:MM:SS format

I want to get time difference in HH:MM:SS format below is the code
if time diff in seconds it should display l
like 00:00:35
In minutes :00:30:35
In Hrs :01:30:35
$start_date = new DateTime($var1[TIME]);
$end_date = new DateTime($var2[TIME]);
$interval = $start_date->diff($end_date);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$seconds = $interval->format('%s');
echo $diff = $hours * 60 + $minutes + $seconds;
use
$hours = $interval->format('%H');
$minutes = $interval->format('%I');
$seconds = $interval->format('%S');
and concat both three for one variable
or use $interval->format('%H:%I:%S') for single output
//output 00:30:35
You can use strtotime() for time calculation. Here is an example:
$time1 = strtotime('10:55:59');<br>
$time2 = strtotime('10:56:00');<br>
$diff = $time2 - $time1;<br>
echo 'Time 1: '.date('H:i:s', $time1).'\n';<br>
echo 'Time 2: '.date('H:i:s', $time2).'\n';
if($diff){<br>
echo 'Diff: '.date('H:i:s', $diff);<br>
}else{<br>
echo 'No Diff.';<br>
}<br>
Output:
Time 1: 09:00:59<br>
Time 2: 09:01:00<br>
Diff: 00:00:01

Categories