PHP 从不带分隔符的日期时间字符串中减去时间量

问题描述 投票:0回答:1

如何从 PHP 中的日期中删除时间,例如:

20170803173418 我想花 4 分 13 秒的时间来获取新的日期戳,即 20170803173005

我需要使用什么代码才能获得这个?

编辑

我目前有:

$dbTime = $row['aptDeadline'];  // This is the appointment end time stored in DB
$dbTime = new DateTime($dbTime);
$currentTime = date("YmdHis"); // This is the current time
$currentTime = new DateTime($currentTime);
$counterTime = $row['aptDeadline']; //This is the time a countdown clock works from inDB
$counterTime = new DateTime($counterTime);

$difference = $currentTime->diff(new DateTime($dbTime)); // Calculate the time between now and the apt time in the db

我现在需要一些代码,如果 $difference 为正,可以将这个数字从 $counterTime 标记中删除

php datetime time subtraction
1个回答
1
投票

您可以使用 PHP 中 DateTime 类的 modify 方法:

<?php

$time = new \DateTime('20170803173418');
$time->modify('-4 minutes')->modify('-13 seconds');

echo $time->format('YmdHis');

这将打印您想要的结果。

© www.soinside.com 2019 - 2024. All rights reserved.