I have a problem with my website. I have create a countdown timer with php code and JS.
My problem is when I echo my variable abc. it’s ok(echo the right timestamp) but when i find the real end time by using “end time from DB – server currentime + client time” then echo it. The result is only “end time from DB – server currentime” How can i fix this issue?
JS
</script>
<script type="text/javascript">
var get_client_time = Math.round(new Date().getTime()/1000);
<?php $abc = "<script>document.write(get_client_time)</script>"?>
</script>
PHP code
echo $abc . "</br>";
$show=mysql_query("SELECT * FROM `room_lists` WHERE `active` = 1");
while ($array = mysql_fetch_array($show))
{
$room_number = $array['room_number'];
$room_name = $array['room_name'];
$subject = $array['subject'];
$s_group = $array['s_group'];
$active = $array['active'];
$timeStop = $array['timeStop'];
//find real ending time
$endDay = $abc + strtotime($timeStop)-strtotime(date('Y-m-d H:i:s'));
echo $endDay;
die();
......} //blah blah blah code
Sadly you cannot mix PHP with Javascript like that.
The only way you can achieve that is with ajax, use some popular libraries jQuery.
1’st method is to use an background from that is submitted as soon as the client enters the page
eg:
This way you will get on next page the
client_timepost variable that will contain client time.2’nd method is to use ajax and send and receive data on same page: $.post
[EDIT] Both methods a unreliable. So you ca never have something well done based on client PC or browser.