Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding 30 minutes to time formatted as H:i in PHP

Tags:

php

strtotime

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         
like image 519
bateman_ap Avatar asked May 04 '10 16:05

bateman_ap


People also ask

How do you add 30 minutes to a timestamp?

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.

How can I add minutes and minutes in PHP?

For this, you can use the strtotime() method. $anyVariableName= strtotime('anyDateValue + X minute'); You can put the integer value in place of X.

How do I add 5 minutes to a datetime?

$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');

How do you add 1 minute to a datetime?

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.


1 Answers

$time = strtotime('10:00'); $startTime = date("H:i", strtotime('-30 minutes', $time)); $endTime = date("H:i", strtotime('+30 minutes', $time)); 
like image 192
webbiedave Avatar answered Sep 21 '22 22:09

webbiedave