Time calculation in php (add 10 hours)?
strtotime()
gives you a number back that represents a time in seconds. To increment it, add the corresponding number of seconds you want to add. 10 hours = 60*60*10 = 36000, so...
$date = date('h:i:s A', strtotime($today)+36000); // $today is today date
Edit: I had assumed you had a string time in $today - if you're just using the current time, even simpler:
$date = date('h:i:s A', time()+36000); // time() returns a time in seconds already
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.
Add hours in a date in PHP
Use DateTime modify method.
$date = new DateTime("2013-03-17 07:11:00");
$date->modify("+4 hours");
echo $date->format("Y-m-d H:i:s");
DEMO.
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.
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.
How to get time difference in minutes in PHP
Subtract the past most one from the future most one and divide by 60.
Times are done in Unix format so they're just a big number showing the number of seconds from January 1, 1970, 00:00:00 GMT
Adding 1 hour to time variable
Worked for me..
$timestamp = strtotime('10:09') + 60*60;
$time = date('H:i', $timestamp);
echo $time;//11:09
Explanation:
strtotime('10:09')
creates a numerical timestamp in seconds, something like 1510450372
. Simply add or remove the amount of seconds you need and use date()
to convert it back into a human readable format.
$timestamp = strtotime('10:09') + 60*60; // 10:09 + 1 hour
$timestamp = strtotime('10:09') + 60*60*2; // 10:09 + 2 hours
$timestamp = strtotime('10:09') - 60*60; // 10:09 - 1 hour
time()
also creates a numerical timestamp but for right now. You can use it in the same way.
$timestamp = time() + 60*60; // now + 1 hour
Add 24 hours to a text time and calculate remain time with PHP
Using DateTime
<?php
$input = '2017-05-11 21:00:00';
$plus_24hrs = DateTime::createFromFormat('Y-m-d H:i:s', $input)->modify('+24 hour');
echo '+24hrs = ' . $plus_24hrs->format('Y-m-d H:i:s') . PHP_EOL;
$remaining = DateTime::createFromFormat('U', time());
$diff = $remaining->diff($plus_24hrs);
echo 'to go: ' . $diff->format('%hh %im %ss');
Example output:
+24hrs = 2017-05-12 21:00:00
to go: 12h 24m 32s
Demo: https://eval.in/792876
Hope this helps.
PHP calculate half of 8 hour working day
Something like this could work
$pickup = strtotime($employee->timepickup);
$return = strtotime($employee->timereturn);
$timediff = ($return - $pickup) / 3600;
$days = floor($timediff / 8);
$halfday = ($timediff - $days * 8) / 4.5;
$days += $halfday < 1 ? 0.5: 1;
It's a bit crude, but you'll end up with adding a half day if the remainder of the time difference is 4.5 hours or less, and adding a whole day if the remainder is larger than 4.5.
how to add two time more than 24 hours
You have a logical error in your code, you just simply sum every part of the time but the thing you need to do is this :
If number of seconds is superior to 60 you have to get only the result of $number_seconds%60 and add it to your minutes and repeat it with the minutes, let me show you :
function TimeSum($a,$b)
{
list ($hour1, $min1, $sec1) = explode(':', $a);
list ($hour2, $min2, $sec2) = explode(':', $b);
//counting number of seconds and getting extra minutes outs
$total_sec = $sec1+$sec2;
$sumSec = $total_sec%60;
$extra_min = ($total_sec-$sumSec)/60;
//counting number of minutes and getting extra hours outs
$total_min= $min1+$min2+$extra_min;
$sumMin = $total_min%60;
$extra_hr = ($total_min-$sumMin)/60;
//counting number of hours
$sumHour = $hour1 + $hour2 + $extra_hr;
return $sumHour.':'.$sumMin.':'.$sumSec;
}
Related Topics
Updating Column So That It Contains the Row Position
What Does a PHP Function Return by Default
How to Convert Float Value to Integer in PHP
Skip Checkout in Magento for a Downloadable Product
Using Header() to Rewrite Filename in Url for Dynamic PDF
Unpacking an Array of Arguments in PHP
Declaring a Global Variable Inside a Function
Writing JSON Object to .JSON File on Server
Laravel: General Error: 1615 Prepared Statement Needs to Be Re-Prepared
Connecting to Websocket with PHP Client
How to Protect HTML Form from Spammers