Time Subtract in PHP - php

I need help to subtract 2 times in PHP.
Example:
$date1 = 02:40;
$date2 = 00:00;
$finaldate = $date1 - $date2;
the correct answer will be 21:20.

Check it out:
if use $date2 as 24:00
$date1 = new DateTime('02:40');
$date2 = new DateTime('24:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); // 21:20
But if use $date2 as 00:00
$date1 = new DateTime('02:40');
$date2 = new DateTime('00:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); //02:40

use strtotime() and subtract both the times ...
<?php
$date1=strtotime("02:40");
$date2=strtotime("00:00");
$diff= date('H:i', $date2-$date1);
echo "Time Difference : ".$diff;
?>
This will output:
Time Difference : 21:20

Related

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";
?>

Calculate difference between to unix epoch date times?

I need to be able to find out the difference between two unix epoch times.
I am trying this at the moment
$interval = $nextFile-$firstFile;
($nextFile would equal "1452182820", $firstFile would equal "1452004380")
This gets me a result of "178440".
Is taking away two epoch date times away from each other valid? Or should i find the difference another way.
Try This May be help ful
<?php
$nextFile = '1452182820';
$firstFile = '1452004380';
$n = date('d-m-Y H:i:s',$nextFile);
$f = date('d-m-Y H:i:s',$firstFile);
$Date1 = date("Y-m-d", strtotime($n));
$Date2 = date("Y-m-d", strtotime($f));
$datetime1 = new DateTime($Date1);
$datetime2 = new DateTime($Date2);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

How to get difference between days and date

function duration($day, $date)
{
$date1 = $date;
//$date2 = date_create("Y/m/d");
//$date1 = date_create("Y/m/d", strtotime($date));
//echo $date1;
//echo $date2;exit;
$date1 = date_create($date);
$date2 = date_create(date('Y-m-d'));
$diff = date_diff($date1, $date2);
return $diff->format("%a");
}
I want difference between days ex. I have entered 10 days and date is 17-12-2015 than I must want 27-12-2015 so how to get it?
Hello Please try this,
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
this will help you to add number of days, month etc to date.
Thanks
Amit

Add current time to DateTime

I would like to add 5 days and current time to a date, which I have in string.
$date = new DateTime('2013-11-21');
date_add($date, date_interval_create_from_date_string('5 days'));
$curtime = date('H:i:s');
How to add current time to DateTime, or is there any other better way how to do it?
Just edit your last lane - I think it is the most objective solution to your problem. The rest of your code is correct.
$curtime = $date->format('Y-m-d H:i:s');
Remember that your second lane is just an alias to:
$date->add(DateInterval::createFromDateString('5 days'));
So the full code would be:
$date = new DateTime('2013-11-21');
$date->add(DateInterval::createFromDateString('5 days'));
$curtime = $date->format('Y-m-d H:i:s');
EDIT: I've just read your question again and you ask about adding current time to this date. If you want to add time, then you need to create it from current date. It's not the perfect solution, but I'm still working on it:
$now = new DateTime(date('1970-01-01 H:i:s'));
$date->add(DateInterval::createFromDateString($now->getTimestamp() . ' seconds'));
$curtime = $date->format('Y-m-d H:i:s');
echo $curtime;
EDIT2: I've corrected it much more, look at this code:
$date = new DateTime('2013-11-21');
$date->add(DateInterval::createFromDateString('5 days'));
$now = new DateTime('now');
$today = new DateTime(date('Y-m-d'));
$time = $today->diff($now);
$date->add($time);
echo $date->format('Y-m-d H:i:s');
EDIT3: And remember about time zones:
$date = new DateTime('2013-11-21', new DateTimeZone('Europe/Warsaw'));
$date->add(DateInterval::createFromDateString('5 days'));
$now = new DateTime('now', new DateTimeZone('Europe/Warsaw'));
$today = new DateTime(date('Y-m-d'), new DateTimeZone('Europe/Warsaw'));
$time = $today->diff($now);
$date->add($time);
echo $date->format('Y-m-d H:i:s');
And fiddle: http://sandbox.onlinephpfunctions.com/code/0080d18d18dd7e2fefa7dea7d961087f14ceb3df
You can add 5 days like this:
$nextX = time() + (5 * 24 * 60 * 60);
5 days * 24 hours * 60 mins * 60 secs
Try:
$date = '2013-11-21';
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);
Try this
$hour_two = "2013-11-21";
$date = strtotime($hour_two);
$hour_two = $date + (5 * 24 * 60 * 60);
$hour_two=date('Y-m-d',$hour_two);
$currenttime = date('H:i:s');
$h = strtotime($currenttime);
$minute = date("i", $h);
$second = date("s", $h);
$hour = date("H", $h);
$new_time = $hour_two." ".$hour.":".$minute.":".$second;
echo $new_time."<br/>"; // here is your final date time
$timeto_string=strtotime($new_time); // test using strtotime
echo date('Y-m-d H:i:s',$timeto_string); // print by formating
$curtime = date('H:i:s');
$date = #date("Y-m-d H:i:s",strtotime($mydate.$curtime)) ;

Getting the difference between two time/dates using php?

I am wanting to find out the time difference in minutes between two dates which is in the format d-m-Y H:i (14-04-2009 12:15) using php?
Parse the times into timestamps using strtotime() and then simply subtract one from the other.
After that you can get the number of minutes, days and so on by using math functions.
For example:
// $date1 and $date2 are given
// the difference is in seconds
$difference = strtotime($date1) - strtotime($date2);
// getting the difference in minutes
$difference_in_minutes = $difference / 60;
Reference: strtotime()
date_default_timezone_set('Asia/Kolkata');
$currentDateTime = date('m/d/Y H:i:s');
$model_current_time = date('Y-m-d H:i:s',
strtotime($currentDateTime));
echo $model_current_time."------";
$date = DateTime::createFromFormat('d/m/Y h:i:s A',
$row['model_creation_time']);//get from resouses
$new_date_format = $date->format('m/d/Y H:i:s');
$model_creation_time = date('Y-m-d H:i:s',
strtotime($new_date_format));
echo $model_creation_time;
$datetime1 = new DateTime($model_current_time);
$datetime2 = new DateTime($model_creation_time);
$interval = $datetime1->diff($datetime2);
echo $interval->d;
echo $interval->h;
echo $interval->s;

Categories