I have some PHP script that echos how much time is left until an event. The time is outputted in a format that looks like 23h 15m 4s
Here is my PHP code:
$now = new DateTime();
$future_date = new DateTime($res['post_time']); # post_time is the time of event
$interval = $future_date->diff($now);
$time_until = rtrim(sprintf("%s%s%s",
$interval->h > 0 ? $interval->h . "h " : "",
$interval->i > 0 ? $interval->i . "m " : "",
$interval->s > 0 ? $interval->s . "s " : ""
)
); # Format time to not have '0'
echo "Time until event " . $time_until;
Is there a way to have the time update every second in Javascript? I imagine that the echo will look something like
echo "Time until event <span id='countdown'>" . $time_until . "</span>";
where the JS will do something with #countdown
?