A PHP+JS real-time display time problem _javascript skills

Source: Internet
Author: User

Let's take a look at this code first.

$ (function () {

setinterval (function () {
var timer = ' <?php echo date (' y-m-d h:i:s ', Time ())?> ';
$ ("#test"). Append (timer);
},500);
};

This piece of code where there is a problem, why the page is loaded after the page has been that time will not change it? What is the difference between PHP and JS loading mechanism?

This is wrong, the PHP code is interpreted before the output, the browser received the code should be like this

$ (function () {
 
setinterval (function () {
var timer = ' 2015-01-02 12:02:02 ';
$ ("#test"). Append (timer);
},500);
};

So, when you're updating a fixed value,
Modified as follows

An extension to date that converts date to a string/month (M) in the specified format, days (d), hours (h), minutes (M), seconds (s), quarter (q) can be used in 1-2 placeholders,//year (Y) can be 1-4 placeholders, milliseconds (s) only 1 placeholders (yes 1-3 digits)//Example://(New Date ()). Format ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423//(New Date ()).  Format ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function (FMT) {//author:meizz var o = {"m+": This.getmonth () + 1,//month "d+": this.getdate (),//day "h+": this.gethours (),//hr "m+": This.get Minutes (),//"s+": This.getseconds (),//sec "q+": Math.floor ((This.getmonth () + 3)/3),//quarter "s": This.get
  milliseconds ()//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]): (("+ 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);
 });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.