AlexGentili Posted November 12, 2021 Share Posted November 12, 2021 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'); Quote Link to comment https://forums.phpfreaks.com/topic/314214-sum-of-time-with-cents/ Share on other sites More sharing options...
ginerjm Posted November 12, 2021 Share Posted November 12, 2021 cents? As in "dollars and cents"? Added to a datetime value? Why ever would one want to do that? Quote Link to comment https://forums.phpfreaks.com/topic/314214-sum-of-time-with-cents/#findComment-1591982 Share on other sites More sharing options...
Barand Posted November 12, 2021 Share Posted November 12, 2021 AFAIK, DateTime and DateInterval only work with whole seconds. If you want to go smaller you'll need microtime() Quote Link to comment https://forums.phpfreaks.com/topic/314214-sum-of-time-with-cents/#findComment-1591983 Share on other sites More sharing options...
Psycho Posted November 12, 2021 Share Posted November 12, 2021 I assume you mean milliseconds. Can you describe what you are trying to accomplish? I.e. do you need\want to include milliseconds or is the problem that there is input data with milliseconds that is causing a failure? The solution to your problem could be very different based on what you are trying to accomplish. The manual shows that the DateTime class does support milliseconds. So, "where" in your code are you wanting to support milliseconds? Will it be the $date, will it be the defined interval, and/or do you need the milliseconds included in the output??? Need some more info. Quote Link to comment https://forums.phpfreaks.com/topic/314214-sum-of-time-with-cents/#findComment-1591987 Share on other sites More sharing options...
Psycho Posted November 12, 2021 Share Posted November 12, 2021 Apparently the DateInterval class supports milliseconds, but the default method does not support it as an input value. You need to instead use the createFromDateString class of that method // convert your date to DateTime object $date = '10:00:00.500000'; $dt = new DateTime($date); // convert your period to $interval = '00:25:10.300000'; //Extract time parts list($hours, $minutes, $totalSeconds) = explode(':', $interval); list($wholeSeconds, $milliSeconds) = explode('.', $totalSeconds); //Create interval with milliseconds $intervalString = "{$hours} hours + {$minutes} minutes + {$wholeSeconds} seconds + {$milliSeconds} microseconds"; $interval = DateInterval::createFromDateString($intervalString); // Add interval to date $dt->add($interval);// Format date as you needecho $dt->format('H:i:s'); echo $dt->format('Y-m-d\TH:i:s.u'); //Output: 2021-11-12T10:25:10.800000 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/314214-sum-of-time-with-cents/#findComment-1591990 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.