This article is intended to solve the problem of real-time display time of php + js, mainly because I had a problem with my understanding at the time. maybe you have the same situation as me, let's take a look at this code.
$(function(){setInterval(function(){var timer = '<?php echo date("Y-m-d H:i:s",time()) ?>';$("#test").append(timer);},500);});
Where is the problem with this code? why does the webpage remain unchanged after the webpage is loaded? What is the difference between the loading mechanism of php and js?
This is wrong. The php code is explained before output, and the code received by the browser should look like this.
$(function(){ setInterval(function(){var timer = '2015-01-02 12:02:02';$("#test").append(timer);},500);});
In this way, when you regularly update a value
Modify as follows:
// Extend the Date to String // month (M), Day (d), Hour (h), and minute (m) in the specified format), second (s), quarter (q) can use 1-2 placeholders, // year (y) can use 1-4 placeholders, millisecond (S) only one placeholder (a number ranging from 1 to 3) can be used. // example: // (new Date ()). format ("yyyy-MM-dd hh: mm: ss. S ") => 08:09:04. 423 // (new Date ()). format ("yyyy-M-d h: m: s. S ") ==>. 18 Date. prototype. format = function (fmt) {// author: meizz var o = {"M +": this. getMonth () + 1, // month "d +": this. getDate (),// Day "h +": this. getHours (), // hour "m +": this. getMinutes (), // minute "s +": this. getSeconds (), // second "q +": Math. floor (this. getMonth () + 3)/3), // quarter "S": this. getMilliseconds () // millisecond}; if (/(y + )/. test (fmt) fmt = fmt. replace (RegExp. $1, (this. getFullYear () + ""). substr (4-RegExp. $1. length); for (var k in o) if (new RegExp ("(" + k + ")"). test (fmt) fmt = fmt. replace (RegExp. $1, (RegExp. $1. length = 1 )? (O [k]): ("00" + o [k]). substr ("" + o [k]). length); return fmt;} $ (function () {setInterval (function () {var timer = new Date (). format ("yyyy-MM-dd HH: mm: ss"); $ ("# test "). append (timer) ;}, 500 );});