tags:

views:

48

answers:

3
+1  Q: 

PHP - time adjust

Hi,

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

+1  A: 

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.

Brad F Jacobs
Since a UNIX timestamp is just a number of seconds, just do `$ts - 4` to remove 4 seconds from it.
Matthew Scharley
+3  A: 

You can use strtotime() like this :

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

which will return the timestamp you need.

Soufiane Hassou
+1  A: 

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.

SickAnimations