The following is really easy way to add days, minutes, hours and seconds to a time using PHP. Using the date function to set the format of the date to be returned then using strtotime to add the increase or decrease of time then after a comma use another strtotime passing in the start date and time. Show
Times can be entered in a readable way:
To sub-tract time its the same except a - is used instead of a + I have a form that receives a time value:
The time is in this format - 9:15:00 - which is 9:15am. I then need to add 15 minutes to this and store that in a separate variable but I'm stumped. I'm trying to use strtotime without success, e.g.:
but that won't parse. asked Dec 13, 2013 at 0:32
user982124user982124 4,32413 gold badges60 silver badges133 bronze badges 3 Your code doesn't work (parse) because you have an extra
Get an editor that will syntax highlight and show unmatched parentheses, braces, etc. To just do straight time without any TZ or DST and add 15 minutes (read zerkms comment):
Still, the answered Dec 13, 2013 at 0:51
3 Though you can do this through PHP's time functions, let me introduce you to PHP's
Note that if what you are looking to do is basically provide a 12 or 24 hours clock functionality to which you can add/subtract time and don't actually care about the date, so you want to eliminate possible problems around daylights saving times changes an such I would recommend one of the following formats:
Note the answered Dec 13, 2013 at 0:42
Mike BrantMike Brant 69.5k10 gold badges97 silver badges102 bronze badges 4 strtotime returns the current timestamp and date is to format timestamp
This will add 15 mins to the current time answered Dec 13, 2018 at 10:24
Nishan BNishan B 4474 silver badges5 bronze badges To expand on previous answers, a function to do this could work like this (changing the time and interval formats however you like them according to this for function.date, and this for DateInterval): (I've also written an alternate form of the below function here.)
answered Apr 4, 2017 at 20:21
get After 20min time and date
20 min ago Date and time
answered Aug 28, 2021 at 20:22
In one line
answered Jun 24 at 1:40
RuydoRuydo 3096 silver badges17 bronze badges You can use below code also.It quite simple.
answered Oct 11, 2017 at 13:12
Bhavesh PatelBhavesh Patel 991 gold badge2 silver badges12 bronze badges Current date and time
15 min ago Date and time
DaFois 2,1868 gold badges23 silver badges39 bronze badges answered Jun 7, 2018 at 12:26
Quite easy
answered Sep 15, 2020 at 13:28
LightwaxxLightwaxx 4891 gold badge6 silver badges16 bronze badges 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 you add hours on Strtotime?You can use DateTime::modify to add time, but I would just do time()+10800 . Show activity on this post. $time = new DateTime("+ 3 hour"); $timestamp = $time->format('Y-M-d h:i:s a');
What does time () do in PHP?The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
What is timestamp format in PHP?What is a TimeStamp? A timestamp in PHP is a numeric value in seconds between the current time and value as at 1st January, 1970 00:00:00 Greenwich Mean Time (GMT). The value returned by the time function depends on the default time zone. The default time zone is set in the php.
|