两个日期之间的分钟数 [英] Number of Minutes between two dates

查看:71
本文介绍了两个日期之间的分钟数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我的约会日期是

$future_time_ending = "2012-09-21 12:12:22"

如何计算当前时间和$future_time_ending之间的分钟数?

How do i work out the number of minutes between the current time and the $future_time_ending?

谢谢

推荐答案

一种方法:

$minutes = (strtotime("2012-09-21 12:12:22") - time()) / 60;

strtotime将日期转换为Unix时间戳-自Unix时代以来的秒数.减去当前时间戳,您将获得当前时间和未来时间之间的秒数.除以60,结果以分钟为单位.

strtotime converts the date to a Unix timestamp - the number of seconds since the Unix epoch. Subtract the current timestamp and you have the number of seconds between the current time and the future time. Divide by 60 and the result is in minutes.

如果您不确定在将来比较的时间,请取绝对值以获得正数:

If you don't know for certain the time you're comparing is in the future, take the absolute value to get a positive number:

$minutes = abs(strtotime("2012-09-21 12:12:22") - time()) / 60;

为了完整回答我的问题,PHP中提供了更详尽的OO方法:

Just to be complete in my answer, there is a more elaborate OO approach available in PHP:

$time = new DateTime("2012-09-21 12:12:22");
$diff = $time->diff(new DateTime());
$minutes = ($diff->days * 24 * 60) +
           ($diff->h * 60) + $diff->i;

如果输入时间来自服务器以外的时区,则该功能特别有用.

This is especially useful if the input time is from a time zone other than the server's.

这篇关于两个日期之间的分钟数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆