Sum of time with cents

this works great and does what it should, but if I had a time format with cents, for example
10:00:00.00 I can’t make the change, any ideas?

// convert your date to DateTime object

$date = '10:00:00';
$dt = new DateTime($date);
// convert your period to 
DateInterval$hours = '00:25:10'; 

/* this data dynamic */

$parts = explode(':', $hours);
$interval = new DateInterval('PT' . (int)$parts[0] . 'H' . $parts[1] . 'M' . $parts[2] . 'S');

// Add interval to date
$dt->add($interval);// Format date as you needecho $dt->format('H:i:s');

Well, I have no idea why you would explode hours that way. There are dozens of PHP functions to handle time intervals. But, you can reformat time by just using the format() function.
But, something like this would work:

date('g:ia', strtotime($date))

OR, just remove it from the $date itself. If the $date is 24 hour version the first 5 chars are the hour and minute. Just take the substring of the first 5…

$a = new DateTime(‘10:00:00.00’);
$b = new DateTime(‘10:01:02.15’);
$interval1 = $a->diff($b);
$parziale = $interval1->format("%H:%I:%S.%F");
$result_parziale = preg_replace("/(?<=\.[0-9]{2})[0]+$/","",$parziale);
echo $result_parziale."
";

$e = new DateTime(‘00:00:00.00’);
$f = clone $e;
$e->add($interval1);

$result=$f->diff($b)->format("%H:%I:%S.%F");
$result = preg_replace("/(?<=\.[0-9]{2})[0]+$/","",$result);
echo $result;

Well, not sure what that code is. First, datetime functions are not 10:00:00.00… That is NOT a standard date-time format. Here are a couple examples of standard ones:
2000-01-01 00:00:00
2000/01/01 00:00:00
01-01-2000
etc…

Time only is hours-minutes-seconds. Therefore, time-wise, 10:00:00.00 does not make sense.
Standard time is either hours-minutes-seconds or it is just miliseconds such as 234213
Perhaps you should explain what 10:00:00.00 really means and where you get this false datetime…

Oh, here is a site that explains datetime functions. Look at the examples and you will see how standard inputs look. We can help once we understand where you are getting these inputs. DateTime

Sponsor our Newsletter | Privacy Policy | Terms of Service