English 中文(简体)
使用 php [重复] 获取两个日期之间的总时间差
原标题:Get total time difference between two dates using php [duplicate]
  • 时间:2012-05-22 06:13:53
  •  标签:
  • php
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
How to calculate the difference between two dates using PHP?

这里我提到两次日期

2008-12-13 10:42:00

2010-10-20 08:10:00

我想以( h: m: s) 格式获得总时间差

问题回答

如果您正在使用或能够使用PHP 5.3.x, 或以后使用PHP 5.3.x, 您可以使用它的 DateTime 对象功能:

$date_a = new DateTime( 2010-10-20 08:10:00 );
$date_b = new DateTime( 2008-12-13 10:42:00 );

$interval = date_diff($date_a,$date_b);

echo $interval->format( %h:%i:%s );

您可以以多种方式使用格式, 一旦您在 DateTime 对象中找到日期, 您就可以利用许多不同的功能, 例如通过普通操作器进行比较 。 更多信息请参见 手册 : < a href=" https:// www. php. net/ manual/ en/ datetime. diff. php" rel = “ nofollown noreferrer" > https:// www. php. net/ manual/ en/ datetime. diff. php < / a >

使用什么 :

$seconds = strtotime("2010-10-20 08:10:00") - strtotime("2008-12-13 10:42:00");

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

您现在可以格式化方式格式化

您可以使用“https://www.php.net/manual/en/formation.strtotime.php' rel=“不跟随 noreferrer”>strtotime 函数 来将时间转换为整数并减去它们。

$time1 = strtotime("2008-12-13 10:42:00");
$time2 = strtotime("2010-10-20 08:10:00");

$diff = $time2-$time1;
// the difference in int. then you can divide by 60,60,24 and 
// so on to get the h:m:s out of it




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签