php 如何计算PHP中的时差?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2920335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:08:32  来源:igfitidea点击:

How to calculate time difference in PHP?

phpdate

提问by php learner

I have to calculate date time difference, how to do that in PHP? I need exact hours, mins and secs. Anybody have the scripts for that?

我必须计算日期时差,如何在 PHP 中做到这一点?我需要确切的小时、分钟和秒。有人有这方面的脚本吗?

回答by vascowhite

Use the diff() methodof PHP's DateTime classlike this:-

像这样使用PHP 的DateTime 类diff() 方法:-

$lastWeek = new DateTime('last thursday');
$now = new DateTime();
var_dump($now->diff($lastWeek, true));

This will give you a DateIntervalobject:-

这会给你一个DateInterval对象:-

object(DateInterval)[48]
  public 'y' => int 0
  public 'm' => int 0
  public 'd' => int 2
  public 'h' => int 11
  public 'i' => int 34
  public 's' => int 41
  public 'invert' => int 0
  public 'days' => int 2

Retrieving the values you want from that is trivial.

从中检索您想要的值是微不足道的。

回答by Vimard

Check this.. This should work

检查这个..这应该工作

<?php

function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

//Usage :
echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");

?> 

回答by James Williams

This should work, just replace the times in the time diff with the time the task started and the current time or the time the task had ended. For a continuous counter the start time would be stored in a database, or for elapsed time for a task the end time would as well

这应该可以工作,只需将时间差异中的时间替换为任务开始的时间和当前时间或任务结束的时间。对于连续计数器,开始时间将存储在数据库中,或者对于任务的经过时间,结束时间也将存储在数据库中

function timeDiff($firstTime,$lastTime){
   // convert to unix timestamps
   $firstTime=strtotime($firstTime);
   $lastTime=strtotime($lastTime);

   // perform subtraction to get the difference (in seconds) between times
   $timeDiff=$lastTime-$firstTime;

   // return the difference
   return $timeDiff;
}

//Usage :
$difference = timeDiff("2002-03-16 10:00:00",date("Y-m-d H:i:s"));
$years = abs(floor($difference / 31536000));
$days = abs(floor(($difference-($years * 31536000))/86400));
$hours = abs(floor(($difference-($years * 31536000)-($days * 86400))/3600));
$mins = abs(floor(($difference-($years * 31536000)-($days * 86400)-($hours * 3600))/60));#floor($difference / 60);
echo "<p>Time Passed: " . $years . " Years, " . $days . " Days, " . $hours . " Hours, " . $mins . " Minutes.</p>";

回答by Babiker

For something like that use the built int time() function.

对于类似的事情,请使用内置的 int time() 函数。

  • Store the value of time();something like 1274467343which is the number of seconds scince
  • When ever needed retrive the value and assign it to $erlierTime.
  • Assign time()again to the latter stop time you would want, lets just call it $latterTime
  • 存储time();类似1274467343秒数的值
  • 在需要时检索该值并将其分配给$erlierTime.
  • time()再次分配给您想要的后一个停止时间,让我们称之为$latterTime

So now you have something like $erlierTimeand $latterTime.

所以现在你有类似$erlierTime和的东西$latterTime

No just do $latterTime - $erlierTimeto get the difference in seconds and then do your divisions to get numb of minutes passed, num of hours passed etc.

不只是$latterTime - $erlierTime为了得到秒的差异,然后做你的除法来让几分钟过去的时间麻木,过去的小时数等等。



In order for me or any one to give you a complete script you would need to tell us what your environment is like and what are you working with mysql, sqlite etc... also would need to know how that timer is triggered.

为了让我或任何人为您提供完整的脚本,您需要告诉我们您的环境是什么样的以及您使用 mysql、sqlite 等做什么......还需要知道该计时器是如何触发的。

回答by Sarfraz

The datedifffunction at addedbytes.comlets you to just that easily :)

addedbytes.com 上datediff功能可让您轻松实现 :)

回答by Imran Naqvi

The best solution would be to use your custom code, there are many built-in functions in php which can let you do it easily. Date, strtotimeand time.

最好的解决方案是使用您的自定义代码,php 中有许多内置函数可以让您轻松完成。 日期strtotime时间

  1. First convert any your dates from "Y-m-d H:i:s" format to Linux time-stamp using strtotimefunction.
  2. Make your calculations and use timeto get current time-stamp if applicable.
  3. Convert you calculated time-stamp to dates using datefunction.
  1. 首先使用strtotime函数将任何日期从“Ymd H:i:s”格式转换为 Linux 时间戳。
  2. 如果适用,请进行计算并使用时间获取当前时间戳。
  3. 使用日期函数将您计算的时间戳转换为日期。

回答by Tarun

You have to convert your start datetime and endtime through strtotime.

您必须通过 strtotime 转换开始日期时间和结束时间。

after that substract the starttime from the end time.

之后从结束时间减去开始时间。

after that pass that difference to date or time format that you needed...

之后将差异传递给您需要的日期或时间格式...

so you will get the exact difference between two date.

所以你会得到两个日期之间的确切差异。