GOAL: Add X amount of seconds to existing time stamp (H:i:s) and convert it into a human readable non-military (24:00 Hr) AM/PM time stamp (‘3:15 pm’). IF possible, I want to nix the leading zero in instances where timestamp would otherwise read ’03:15′.
PROBLEM: All tests return the same results adjusting for variable time added return the same final result: 16:00:00 and/or 04:00 pm
THE ASK: Guidance on where / how my code is failing and how to correct it. I have googled and searched stack, etc. I have run experiments for a few hours and I always get the same result regardless of variables I feed into the test. I have attempted 23 tests and continually returned the same result.
ENVIRONMENT: MAMP, PHP 8.1, Bootstrap
CURRENT RESULT:
01:43:21
1652258601
1652258807
16:00:00 PM
4:00 PM
THE CODE:
$myTIME = '01:43:21'; $staging .= $myTIME . '<br>'; $this_time = strtotime($myTIME); $staging .= $this_time . '<br>'; $add = rand(23, 218); $stamp = $this_time + $add; $staging .= $stamp . '<br>'; $staging .= date("H:i:s A", strtotime($stamp)) . '<br>'; # $stamp = '23:00'; $stamp = new DateTime($stamp); echo $dt->format('h:i A'); $est = date('h:i:s', strtotime($stamp)); $staging .= date( 'g:i A', strtotime( $stamp ) );
Advertisement
Answer
$stamp
is already an integer/timestamp, so strtotime($stamp)
fails. Take that off, and the date will format correctly.
$est = date('h:i:s', $stamp);