javascript - How to get SERVER date using Ajax call? -
right trying date of local host. ajax request not working successfully. how do it?
html code: has ajax code in script tag.
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <script language=”javascript” type=”text/javascript”> function ajaxfunction() { var xh; try { // firefox, opera 8.0+, safari xh=new xmlhttprequest(); } catch (e) { alert("your browser not support ajax!"); } xh.onreadystatechange=statechanged; xh.open("get","19_time.php",true); xh.send(null); tt=timer_function(); } function statechanged() { if(xh.readystate==4) document.getelementbyid("time").innerhtml=xh.responsetext; } function timer_function(){ var mytime= setinterval(ajaxfunction,1000); } window.addeventlistener('load',ajaxfunction,false); </script> </head> <body> <html> <body> <div id="time" style="height:10%; width:30%; margin:4%; margin-top:10%; border:5px solid #804; font-size:26px; " ></div> </body> </html>
php code: connect server , send result
<?php ///////// database details , add here //// //$dbhost_name = "localhost"; //$username = "root"; // login user id //$password = ""; // login password /////////// end of database details ////// //$con=mysql_connect($dbhost_name,$username,$password) or die("couldn't connect: ".mysql_error()); //mysql_select_db($database,$con) or die("error selecting database: ".mysql_error()); //////// not edit below ///////// $con = mysql_connect('localhost', 'root', ''); $msg=""; $msg .=$con->date('y-m-d h:i:s'); echo $msg; ?>
date() not function in mysql_connect, $con->date() nothing. date() php function.
if you're trying mysql server time:
$con = mysql_connect('localhost', 'root', ''); $qry = "select now()"; $sql_time = $con->mysql_query($qry); echo json_encode($sql_time);
else, if want php server time (should system time) have code:
echo json_encode(date('y-m-d h:i:s'));
when sending response data php client after ajax call, make sure json_encode response.