Adding minutes to date time in PHP
$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');
The ISO 8601 standard for duration is a string in the form of P{y}Y{m1}M{d}DT{h}H{m2}M{s}S
where the {*}
parts are replaced by a number value indicating how long the duration is.
For example, P1Y2DT5S
means 1 year, 2 days, and 5 seconds.
In the example above, we are providing PT5M
(or 5 minutes) to the DateInterval
constructor.
PHP Date Time Current Time Add Minutes
I think one of the best solutions and easiest is:
date("Y-m-d", strtotime("+30 minutes"))
Maybe it's not the most efficient but is one of the more understandable.
Add minutes to current time
Your code is redundant. Why format a timestamp as a string, then convert that string back to a timestamp?
Try
$now = time();
$ten_minutes = $now + (10 * 60);
$startDate = date('m-d-Y H:i:s', $now);
$endDate = date('m-d-Y H:i:s', $ten_minutes);
instead.
PHP Adding 15 minutes to Time value
Your code doesn't work (parse) because you have an extra )
at the end that causes a Parse Error. Count, you have 2 (
and 3 )
. It would work fine if you fix that, but strtotime()
returns a timestamp, so to get a human readable time use date()
.
$selectedTime = "9:15:00";
$endTime = strtotime("+15 minutes", strtotime($selectedTime));
echo date('h:i:s', $endTime);
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):
$endTime = strtotime($selectedTime) + 900; //900 = 15 min X 60 sec
Still, the )
is the main issue here.
Add 'x' number of hours to date
You may use something like the strtotime()
function to add something to the current timestamp. $new_time = date("Y-m-d H:i:s", strtotime('+5 hours'))
.
If you need variables in the function, you must use double quotes then like strtotime("+{$hours} hours")
, however better you use strtotime(sprintf("+%d hours", $hours))
then.
How to add 5 minutes to current datetime on php 5.3
$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);
Now, the result is 2011-04-08 08:34:49 and is stored inside $formatDate
Enjoy! :)
Add minutes to time based on current time
I found the solution, will post it in case someone needs it.
// Order info
$job_description = 'Order '.$order_id;
$order_created = date( 'Y-m-d H:i:s', $order->get_date_created ()->getOffsetTimestamp());
$current_time = date( 'H:i:s', $order->get_date_created ()->getOffsetTimestamp());
$minutes_to_add = 0;
if ($current_time >= "00:00:00" && $current_time < "01:00:00") {
$minutes_to_add = 600;
} elseif ($current_time >= "01:00:00" && $current_time < "02:00:00"){
$minutes_to_add = 615;
} elseif ($current_time >= "02:00:00" && $current_time < "03:00:00"){
$minutes_to_add = 570;
} elseif ($current_time >= "03:00:00" && $current_time < "04:00:00"){
$minutes_to_add = 525;
} elseif ($current_time >= "23:00:00" && $current_time <= "23:59:59"){
$minutes_to_add = 720;
} else {
$minutes_to_add = 30;
}
$order_created_obj = new DateTime($order_created);
$order_created_obj->add(new DateInterval('PT' . $minutes_to_add . 'M'));
$job_pickup_datetime = $order_created_obj->format('Y-m-d H:i:s');
$current_time = date( 'H:i:s', $order->get_date_created ()->getOffsetTimestamp());
$minutes_to_add_de = 0;
if ($current_time >= "00:00:00" && $current_time < "01:00:00") {
$minutes_to_add_de = 630;
} elseif ($current_time >= "01:00:00" && $current_time < "02:00:00"){
$minutes_to_add_de = 645;
} elseif ($current_time >= "02:00:00" && $current_time < "03:00:00"){
$minutes_to_add_de = 600;
} elseif ($current_time >= "22:00:00" && $current_time < "23:00:00"){
$minutes_to_add_de = 795;
} elseif ($current_time >= "23:00:00" && $current_time <= "23:59:59"){
$minutes_to_add_de = 750;
} else {
$minutes_to_add_de = 60;
}
Add time to date time in php
You can try with strtotime
and date
function
$dateTime = '2016-09-08 21:00';
echo date( "Y-m-d h:i", strtotime( "2016-09-08 21:00 +4 hours" ) );
echo date( "Y-m-d H:i", strtotime( "2016-09-08 21:00 +1 hours 40 minutes" ) );
adding 30 minutes to date
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
Related Topics
Change Name of Laravel's Created_At and Updated_At
PHP Artisan Migrate Throwing [Pdo Exception] Could Not Find Driver - Using Laravel
Can't Execute PHP Script Using PHP Exec
How to Call a Python Script from PHP
Can Closing the Browser Terminate the PHP Script on the Server
Can Anyone Get Access to My PHP Source Code
How to Install an Older Version of PHPunit Through Pear
Checking for an Empty Result (Php, Pdo, and MySQL)
Using Ajax in a Wordpress Plugin
Warning: "Continue" Targeting Switch Is Equivalent to "Break". Did You Mean to Use "Continue 2"
How to Break an Outer Loop with PHP
Recursive Function to Get All the Child Categories