如何获得两个日期之间的时间差(“Y-m-d H:i:s”)格式?

时间:2021-12-21 00:26:30

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

我试着得到两个日期的差,然后把它返回到一分钟。日期和时间是按日期(“Y-m-d H:i:s”)格式进行的。但似乎我做错了。我做到了

$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"

这是对一个非对象的成员函数diff()的调用

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

因为我对日期格式不够好。所以请帮助我。

What is the way to go about this?

怎么做呢?

3 个解决方案

#1


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

有关详细信息,请参见此链接:http://www.php.net/manual/en/book.datetime.php。

#2


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

演示

#3


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

$ time1 =日期(“Y-m-d H:我:”);$ time2 =日期(“2014-01-13 08:18:25”);


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

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

$年=地板((秒)/(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));

#1


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

有关详细信息,请参见此链接:http://www.php.net/manual/en/book.datetime.php。

#2


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

演示

#3


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

$ time1 =日期(“Y-m-d H:我:”);$ time2 =日期(“2014-01-13 08:18:25”);


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

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

$年=地板((秒)/(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));