I want to get the time difference between 2 dates in minutes. The 2 dates are in the following format
date1='05-11-2012 11:25:00'
date2='06-11-2012 17:45:00'
$datetime1 = new DateTime('05-11-2012 11:25:00');
$datetime2 = new DateTime('06-11-2012 17:45:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days'); //return +1 days
//or
$datetime1 = date_create('05-11-2012 11:25:00');
$datetime2 = date_create('06-11-2012 17:45:00');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days'); //return +1 days
for php <5.3
$date1 = '05-11-2012 11:25:00';
$date2 = '06-11-2012 17:45:00';
$diff = floor(abs( strtotime( $date1 ) - strtotime( $date2 ) )/(60*60*24));
printf("%d days\n", $diff); //return 1 days
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
For further reference please visit the following link
http://php.net/manual/en/datetime.diff.php
You neglected to indicate what format you'd like the time difference expressed in. If you're willing to work in seconds (which are easily converted), you could simply convert each date to a timestamp and then take the absolute difference using basic math. For instance, give $date1 = '05-11-2012 11:25:00' and $date2 = '06-11-2012 17:45:00':
$diff_seconds = abs( strtotime( $date1 ) - strtotime( $date2 ) );
And you could do whatever you like with the result.
$start_time = strtotime( "2012-10-12 11:35:00" );
$end_time = strtotime( "2012-10-13 12:42:50" );
echo round ( abs( $end_time - $start_time ) / 60,2 ). " minute";
This is different between two dates in MINUTES.
Related
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";
?>
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');
I have the following:
$dateStart = get_post_meta( get_the_ID(), 'startdate' , true);
$dateStart = DateTime::createFromFormat('d/m/Y h:i:s', $dateStart);
$dStart = $dateStart->format('d/m/y');true);
$dEnd = date("d/m/y");
echo $dStart .'-'.$dEnd;
This outputs 12/06/15-28/10/15 but for the life of me I can't work out how to get the difference in days between the two dates.
Any advice? I've tried a few things but they error out each time.
Thanks
The most secure way I found to compute the number of days between a date DateTime $a and another on DateTime $b is to rely on their timestamps at midnight:
protected static function diffDays(DateTime $a, DateTime $b)
{
return round(
($a->setTime(0, 0, 0)->getTimestamp() - $b->setTime(0, 0, 0)->getTimestamp())
/ 86400 // 60 * 60 * 24 = 86400s in 24h
);
}
Work very well for me :)
Note: I use hard coded value 86400 in order to reduce useless processing time calculating 60 * 60 * 24
Use this(Working Example)
date_default_timezone_set("Asia/Colombo");
$dateStart = get_post_meta( get_the_ID(), 'startdate' , true);
$datetime1 = new DateTime(date('Y-m-d', $dateStart));
$datetime2 = new DateTime(date('Y-m-d', $dateStart)));
$interval = $datetime1->diff($datetime2);
$date_diff = $interval->format('%R%a days');
echo "Difference is ".$date_diff
Use DateTime::diff - http://php.net/manual/en/datetime.diff.php
$dateStart = DateTime::createFromFormat('d/m/Y h:i:s', $dateStart);
$dateEnd = new DateTime();
$difference = $dateStart->diff($dateEnd);
Also note that not passing the end to diff() will assume now. So this would work also, in your particular example:
$dateStart = DateTime::createFromFormat('d/m/Y h:i:s', $dateStart);
$difference = $dateStart->diff();
$daysBefore = round(abs(strtotime(Date("l, d F Y"))-strtotime($dateStart))/86400);
$daysAfter = round(abs(strtotime(Date("l, d F Y"))-strtotime($dateEnd))/86400);
This seemed to be the only way I could get it to work
I searched online but couldn't see much help on this.
Is there a ready to use function to do this in PHP that I haven't found?
I want to calculate the number of days between 2 variable dates e.g.:
$date1 = '01/08/2014'; $date2 = '07/08/2014;
I tried the below but $count is null:
<?php
$date1 = '01/01/14';
$date2 = '07/01/14';
$count = $date1->diff($date2);
echo $count;
?>
Better:
$from = DateTime::createFromFormat("d/m/Y",$date1);
$to = DateTime::createFromFormat("d/m/Y",$date2);
$diff = $from->diff($to,true);
$days = $diff->days;
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24));
Or you can even use PHP's date_diff function like this:
$interval = $date1->diff($date2);
http://php.net/manual/en/datetime.diff.php
Use DateTime::diff (aka date_diff):
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
Or:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
You can then get the interval as a integer by calling $interval->days.
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;