teepusink
teepusink

Reputation: 28892

PHP - time adjust

I have a set of time in this format. 01:00:04

How can I adjust the time to deduct 4 second from it?
Was trying to parse it but is the a quick way to do it using datetime or something?

Thanks,
Tee

Upvotes: 1

Views: 254

Answers (3)

TimCinel
TimCinel

Reputation: 2000

If you want the result in the same format, you could use strtotime() and date() to do:

$delayed_times = array();
foreach ($original_times as $original_time) {
    $delayed_times[] = date("H:i:s", strtotime($original_time) - 4);
}

Now this is assuming you're talking about time in a time-of-day sense. If you have a list of times that are durations, this would break for durations equal to or greater than 24 hours.

Upvotes: 1

Jim
Jim

Reputation: 18853

I am not sure if the format is a typo, but perhaps strtotime() maybe what you are looking for. It should convert it to a UNIX Timestamp where you can add or subtract seconds to it etc.

Upvotes: 1

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

You can use strtotime() like this :

strtotime("-4 seconds", $reference_time);

which will return the timestamp you need.

Upvotes: 4

Related Questions