Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time calculation in php (add 10 hours)?

Tags:

php

time

addition

People also ask

How can I calculate total hours in php?

There are two ways to calculate the total time from the array. Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time in h:m:s format.

How can I add hours and minutes in php?

php function CalculateTime($times) { $i = 0; foreach ($times as $time) { sscanf($time, '%d:%d', $hour, $min); $i += $hour * 60 + $min; } if($h = floor($i / 60)) { $i %= 60; } return sprintf('%02d:%02d', $h, $i); } $date[] = '02:32'; $date[] = '01:29'; echo CalculateTime($date); ?>

How does php calculate time?

The hours is calculated by dividing the value computed by 3600. The minutes is calculated by dividing the value computed by product of hours calculated and 3600. The seconds is calculated by dividing the value computer by product of minutes and 60. The total time computed is displayed on the console.

How does php calculate 24 hours?

php $starttime = strtotime('2017-05-11 21:00:00'); $starttimeformat = date('Y-m-d H:i:s', $starttime); echo "Current Time:"; echo $starttimeformat; echo '<br/>'; echo '<br/>'; $onedayadedtime_format = date('Y-m-d H:i:s', strtotime('+24 hours', $starttime)); echo "End Time after adding 24 hours:"; echo $ ...


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

$tz = new DateTimeZone('Europe/London');
$date = new DateTime($today, $tz);
$date->modify('+10 hours');
// use $date->format() to outputs the result.

see DateTime Class (PHP 5 >= 5.2.0)


$date = date('h:i:s A', strtotime($today . ' + 10 hours'));

(untested)


You can simply make use of the DateTime class , OOP Style.

<?php
$date = new DateTime('1:00:00');
$date->add(new DateInterval('PT10H'));
echo $date->format('H:i:s a'); //"prints" 11:00:00 a.m

$date = date('h:i:s A', strtotime($today . " +10 hours"));

Full code that shows now and 10 minutes added.....

$nowtime = date("Y-m-d H:i:s");
echo $nowtime;
$date = date('Y-m-d H:i:s', strtotime($nowtime . ' + 10 minute'));
echo "<br>".$date;