日期 php 中的日期差异?

发布于 2024-08-15 13:09:19 字数 197 浏览 4 评论 0原文

php 有没有快速计算日期差异的方法?例如:

$date1 = '2009-11-12 12:09:08';
$date2 = '2009-12-01 08:20:11';

然后进行计算,$date2 减去 $date1

我阅读了 php.net 文档,但没有运气。有没有快速的方法来做到这一点?

Is there a quick way to calculate date difference in php? For example:

$date1 = '2009-11-12 12:09:08';
$date2 = '2009-12-01 08:20:11';

And then do a calculation, $date2 minus $date1

I read php.net documentation, but no luck. Is there a quick way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮坠入山谷 2024-08-22 13:09:19

我建议使用 date->diff 函数,如下例所示:

   $dStart = new DateTime('2012-07-26');
   $dEnd  = new DateTime('2012-08-26');
   $dDiff = $dStart->diff($dEnd);
   echo $dDiff->format('%r%a'); // use for point out relation: smaller/greater

请参阅 http://www.php.net/manual/en/datetime.diff.php

I would recommend to use date->diff function, as in example below:

   $dStart = new DateTime('2012-07-26');
   $dEnd  = new DateTime('2012-08-26');
   $dDiff = $dStart->diff($dEnd);
   echo $dDiff->format('%r%a'); // use for point out relation: smaller/greater

see http://www.php.net/manual/en/datetime.diff.php

心是晴朗的。 2024-08-22 13:09:19

strtotime 会将您的日期字符串转换为 unix 时间戳。 (自 UNIX 纪元以来的秒数。

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$seconds_diff = $ts2 - $ts1;

strtotime will convert your date string to a unix time stamp. (seconds since the unix epoch.

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$seconds_diff = $ts2 - $ts1;
温柔一刀 2024-08-22 13:09:19

下面的代码将通过取出差异来给出天数的输出
两个日期之间..

$str = "Jul 02 2013";
$str = strtotime(date("M d Y ")) - (strtotime($str));
echo floor($str/3600/24);

Below code will give the output for number of days, by taking out the difference
between two dates..

$str = "Jul 02 2013";
$str = strtotime(date("M d Y ")) - (strtotime($str));
echo floor($str/3600/24);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文