2

I need to figure out

  • 5 minutes before given time
  • 10 seconds before given time I have try this code

    echo $time.'<br />'; 
    echo 'fivemin'.$fivemin= date("H:m:s",strtotime("-5 minutes",strtotime($time))); 
    echo '<br />tensec'.$tensec=$datetime_from = date("H:m:s",strtotime("-10 seconds",strtotime($time)));
    

result

15:55:44
fivemin 15:04:44
tensec 15:04:34
Khalid
  • 4,730
  • 5
  • 27
  • 50

1 Answers1

4

You can use the DateTime::sub method for this.

// Set the initial date/time
$date = new DateTime('2015-04-03 12:00:00');

// Go back 5 minutes
$date->sub(new DateInterval('PT5M'));

// $date is now 2015-04-03 11:55:00, go back another 10 seconds...
$date->sub(new DateInterval('PT10S'));

You can also combine the two subs into one (I just split them so you have a better idea of what's going on and you may want to run some actions in between the subs):

$date->sub(new DateInterval('PT5M10S')); // 5 minutes and 10 seconds ago

That will subtract 5 minutes and 10 seconds in 1 go.

You will then end up with $date being a DateTime object, that is now representing 2015-04-03 11:54:50.

Oldskool
  • 34,211
  • 7
  • 53
  • 66