0

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' );
?>
3
  • If you want to change the time without reloading the page, you have to use javascript. Commented Jun 3, 2016 at 8:17
  • yes...with out reloading Commented Jun 3, 2016 at 8:18
  • You can't do that with just PHP because PHP runs on the server. Commented Jun 3, 2016 at 8:19

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.