-3

I'm trying to get the difference between two date-times and return it as a minute. Date & Time are taken in date("Y-m-d H:i:s") format. But it seem i can't get it right. I did it

$time=date("Y-m-d H:i:s");
$time=date("2014-01-13 08:18:25");

$interval = $time->diff($login_time);
$elapsed = $interval->format(%i minutes);
echo $elapsed;

And This is showing a massage "Call to a member function diff() on a non-object"

As I am not good enough with date formatting. So Please help me.

What is the way to go about this?

Mamun Al
  • 11
  • 2
  • 5
  • 3
    Well, first of all, `$time` is not an object, as it says. It is merely a string containg the current date. Also a duplicate, see accepted answer for http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – Mave Jan 13 '14 at 10:18
  • Before putting question here do googling – The Hungry Dictator Jan 13 '14 at 10:25

3 Answers3

0

Try this:

$date1 = new DateTime('2013-01-13 04:10:58');
$datediff = $date1->diff(new DateTime('2013-09-11 10:25:00'));
echo $datediff->i;

For more details see this link : http://www.php.net/manual/en/book.datetime.php

user2936213
  • 1,021
  • 1
  • 8
  • 19
0

Get difference is minutes between two dates:

$now     = new DateTime;
$before  = new DateTime('2014-01-10 08:18:25');
$diff    = $now->diff($before);
$elapsed = $diff->days * 24 * 60 + $diff->h * 60 + $diff->i;

demo

Glavić
  • 42,781
  • 13
  • 77
  • 107
-1

Use the below code for getting time in all expected parameter.

$time1=date("Y-m-d H:i:s");
$time2=date("2014-01-13 08:18:25");


$seconds = strtotime($time1) - strtotime($start);


$year = floor(($seconds)/(60*60*24*365));
$months = floor($seconds / 86400 / 30 );
$week = floor($seconds / 604800);
$days = floor($seconds / 86400);
$hours = floor(($seconds - ($days * 86400)) / 3600);
$minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60);
$seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));

shubomb
  • 672
  • 7
  • 20