Having a nightmare at the moment and just can't see why it isn't working
I have a value in the form H:i (ie 10:00, 13:30) etc called $time
What I want to do is create two new values, $startTime which is 30 mins before $time and $endTime which is 30 mins after $time
I have tried the following but just doesn't seem to want to work
$startTime = date("H:i",strtotime('-30 minutes',$time)); $endTime = date("H:i",strtotime('+30 minutes',$time));
If I pass through 10:00 as $time and echo out both $startTime and $endTime I get:
$startTime = 00:30 $startTime = 01:30
To add 30 minutes to the Date Object first, we get the current time by using the Date. getTime( ) method and then add 30 minute's milliseconds value (30 * 60 * 1000) to it and pass the added value to the Date Object.
For this, you can use the strtotime() method. $anyVariableName= strtotime('anyDateValue + X minute'); You can put the integer value in place of X.
$minutes_to_add = 5; $time = new DateTime('2011-11-17 05:05'); $time->add(new DateInterval('PT' . $minutes_to_add . 'M')); $stamp = $time->format('Y-m-d H:i');
Use the timedelta() class from the datetime module to add minutes to datetime, e.g. result = dt + timedelta(minutes=10) . The timedelta class can be passed a minutes argument and adds the specified number of minutes to the datetime.
$time = strtotime('10:00'); $startTime = date("H:i", strtotime('-30 minutes', $time)); $endTime = date("H:i", strtotime('+30 minutes', $time));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With