Real-time display of a php + js _ javascript skills

Source: Internet
Author: User
Tags echo date
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 );});

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.