Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding 30 minutes to date [closed]

Tags:

So what I need to do is add 30 minutes to the following

date("Ymdhis"); 

I tried this

+strtotime("+30 minutes"); 

however it does not seem to like it. I wondering what the correct why to do this is.

like image 489
RussellHarrower Avatar asked Mar 22 '12 22:03

RussellHarrower


People also ask

How do you add 30 minutes to a timestamp?

strtotime("+30 minutes"); (which is an integer) to that string. date("Ymdhis", strtotime("+30 minutes"));

How do you add time to a date object?

To add hours to a JavaScript Date object, use the setHours() method. Under that, get the current hours and 2 hours to it. JavaScript date setHours() method sets the hours for a specified date according to local time.

How can I add 1 day to current date?

const date = new Date(); date. setDate(date. getDate() + 1); // ✅ 1 Day added console.


1 Answers

Your method of using strtotime should work.

<?php  echo date("Y/m/d H:i:s", strtotime("now")) . "\n"; echo date("Y/m/d H:i:s", strtotime("+30 minutes"));  ?> 

Output

2012/03/22 10:55:45 2012/03/22 11:25:45 // 30 minutes later 

However your method of adding time probably isn't correct. The above will work to add 30 minutes to the current time. Suppose you want to add 30 minutes from a given time, $t, then use strtotime's second parameter, which is used as a base for the calculation of relative dates.

date("Y/m/d H:i:s", strtotime("+30 minutes", $t)); 

http://codepad.org/Z5yquF55

like image 120
Josh Avatar answered Dec 05 '22 04:12

Josh