What are some best practices for integrating PHP and JavaScript to display the current time?

When integrating PHP and JavaScript to display the current time, one best practice is to use PHP to generate the initial time on the server-side and then use JavaScript to update it dynamically on the client-side without refreshing the page. This ensures that the displayed time stays accurate and up-to-date for the user.

<?php
echo '<div id="current-time"></div>';
echo '<script>';
echo 'function updateTime() {';
echo 'var currentTime = new Date();';
echo 'var hours = currentTime.getHours();';
echo 'var minutes = currentTime.getMinutes();';
echo 'var seconds = currentTime.getSeconds();';
echo 'document.getElementById("current-time").innerHTML = hours + ":" + minutes + ":" + seconds;';
echo '}';
echo 'setInterval(updateTime, 1000);'; // Update time every second
echo '</script>';
?>