0

I am trying to store time into variable which type is string, following is script

$time = '00:00:00';
foreach($GameLog->gamelogs->toArray() as $Data){
    $base = strtotime('00:00:00');
    $seconds = (strtotime($time) - $base) + (strtotime($Data['pivot']['time_spent']) - $base);
}

$hours = floor($seconds / 3600);
$minutes = floor($seconds / 60) % 60;
$seconds = $seconds % 60;
$ResultArray['time']   =  sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);    

where is $Data['pivot']['time_spent'] has different times like

00:05:30 00:03:25 00:12:39

By above loop method I am unable to store the time, because it is returning wrong result. how can i fix the issue?

4
  • Possible duplicate of Convert DateTime to String PHP Commented Feb 27, 2019 at 13:02
  • @Dave it's now duplicated you need to read the question properly Commented Feb 27, 2019 at 13:27
  • what's actually wrong with it? Tested and it returns a microtime as expected? Commented Feb 27, 2019 at 17:17
  • and try moving your code inside the foreach loop - all that will do is use the last iteration in the code outside the loop .. Commented Feb 27, 2019 at 17:19

1 Answer 1

-1

Try writing your times with '.' instead of ':' like :

$time = '00.00.00';

See why here

Sign up to request clarification or add additional context in comments.

1 Comment

this is a mix of code-only and link-only - you should add relevant info from the link to keep it on SO for future users (that link may be dead in < 3 months for example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.