简体   繁体   中英

How time automatically change in php?

I am working in time functions in php. I write a code for to display particular timezone time it works good but time not chaning automatically when i refresh the page on that time i will changing. Can any one give solution for this.

<?php
                $indiatimezone = new DateTimeZone("Asia/Kolkata" );
                $date = new DateTime();
                $date->setTimezone($indiatimezone);
                echo "India Time: ";
                echo  $date->format( 'H:i:s A  /  D, M jS, Y' );
?>
Try it with javascript like this:  

<div class="classname">
        <span class="classname" id="clockDisplay"></span>
        </div> 

     <script>
            function renderTime() {
                var currentTime = new Date();
                var diem = "AM";
                var h = currentTime.getHours();
                var m = currentTime.getMinutes();
                var s = currentTime.getSeconds();
                setTimeout('renderTime()',1000);
                if (h == 0) {
                    h = 12;
                }
                else if (h == 12) {
                    diem="PM";
                }

                else if (h > 12) {
                    h = h - 12;
                    diem="PM";
                }


                if (h < 10) {
                    h = "0" + h;
                }
                if (m < 10) {
                    m = "0" + m;
                }
                if (s < 10) {
                    s = "0" + s;
                }
                var myClock = document.getElementById('clockDisplay');
                myClock.textContent = h + ":" + m + ":" + s + " " + diem;
                myClock.innerText = h + ":" + m + ":" + s + " " + diem;
            }
            renderTime();
        </script>

You cannot use PHP to display and update the time without refreshing the page. PHP runs on the server, so when you access your page it generates the output for you and it won't change until you refresh it. However, you can do this with javascript. Here is an example that can help you get started: link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM