php - PHP 中两个时间戳之间的差异

标签 php time mamp strtotime maxlength

在我看来,这应该回显 11 小时,但它返回 12;我错过了什么?

$start_time = "06:00";
$end_time = "17:00";
$LOD = date("H:i", ((strtotime($end_time) - strtotime($start_time))));
echo  "Length of day: " . $LOD . " hours";

更新:在 MAMP 环境中在我的 MBPro 上运行;系统时间设置为 24 小时制。

最佳答案

问题出在这里:

date_default_timezone_set('Asia/Singapore');
echo date('r', strtotime('06:00')); // Mon, 25 May 2015 06:00:00 +0800
date_default_timezone_set('America/Denver');
echo date('r', strtotime('06:00')); // Sun, 24 May 2015 06:00:00 -0600

注意到日期如何因更改时区而移动了一天吗?这是因为您提供给 strtotime() 的日期是相对日期,因此您需要根据一天的开始“基础”它:

echo date('H:i', strtotime('17:00') - strtotime('06:00') + strtotime('00:00'));

或者,使用DateTime :

$t1 = new DateTime('06:00');
$t2 = new DateTime('17:00');
echo $t2->diff($t1)->format('%H:%I');

关于php - PHP 中两个时间戳之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30430354/

相关文章:

php - Wordpress 仪表板未加载

sql - 如何测量 Spark 上查询的执行时间

TYPO3 6.2typo3_src 应该是一个链接

javascript - 如何从动态创建的php文件中读取echo并刷新div中的echo

php - 如何迭代页面上的表单字段并将它们发送到数据库?

java日期格式与xquery xs :date format,不兼容如何解决?

google-chrome - 自签名 SSL 证书不适用于 MAMP 和 Chrome

mysql - 更改 MySQL 端口 - Mac - MAMP

php - 用数组重写 PHP 函数

python - 使用 Python 每 15 分钟检查一次日志文件