murali krishna
murali krishna

Reputation: 21

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' );
?>

Upvotes: 0

Views: 528

Answers (2)

anju
anju

Reputation: 609

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>

Upvotes: 1

xjmdoo
xjmdoo

Reputation: 1736

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.

Upvotes: 0

Related Questions