JoeSaunderson
JoeSaunderson

Reputation: 11

PHP subtract an amount of time from a datetime string with no delimiters

How do I remove time from a date in PHP, for example:

20170803173418 I want to take 4 minutes and 13 seconds away and get the new datestamp that would be 20170803173005

What code do I use to get this?

EDIT

I currently have:

$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

I now need some code that if the $difference is positive, can take this figure away from the $counterTime stamp

Upvotes: 0

Views: 3727

Answers (1)

Thanasis Pap
Thanasis Pap

Reputation: 2051

You can use the modify method of the DateTime class in PHP:

<?php

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

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

This will print the result you want.

Upvotes: 1

Related Questions