2013-08-22 68 views
0

這是我的js/PHP的顯示功能:我需要爲這個js添加一天嗎?

<script type="text/javascript"> 
    $.fn.cycle.defaults.speed = 900; 
    $.fn.cycle.defaults.timeout = 5000; 

    $(function() 
    { 
     $('#demos pre code').each(function() 
     { 
      eval($(this).text()); 
     }); 

     $('#demos2 pre code').each(function() 
     { 
      eval($(this).text()); 
     }); 
    }); 

    $(function($) { 
     var pstOptions = { 
     timeNotation: '12h', 
     am_pm: true, 
     utc: true, 
     utc_offset: <%SETTING_TIMEOFFSET%>, 
     fontFamily: 'Verdana, Times New Roman', 
     fontSize: '11px', 
     foreground: 'white', 

     background: 'black' 
     } 
     $('.jclockPST').jclock(pstOptions); 
    }); 
</script> 

,這是我的全部JS腳本:

/* 
* jQuery jclock - Clock plugin - v 0.2.1 
* http://plugins.jquery.com/project/jclock 
* 
* Copyright (c) 2007-2008 Doug Sparling <http://www.dougsparling.com> 
* Licensed under the MIT License: 
* http://www.opensource.org/licenses/mit-license.php 
*/ 
(function($) { 

    $.fn.jclock = function(options) { 
    var version = '0.2.1'; 

    // options 
    var opts = $.extend({}, $.fn.jclock.defaults, options); 

    return this.each(function() { 
     $this = $(this); 
     $this.timerID = null; 
     $this.running = false; 

     $.fn.jclock.getServerOffset($this); 

     var o = $.meta ? $.extend({}, opts, $this.data()) : opts; 

     $this.timeNotation = o.timeNotation; 
     $this.am_pm = o.am_pm; 
     $this.utc = o.utc; 
     $this.utc_offset = o.utc_offset; 

     $this.css({ 
     fontFamily: o.fontFamily, 
     fontSize: o.fontSize, 
     backgroundColor: o.background, 
     color: o.foreground 
     }); 

     $.fn.jclock.startClock($this); 

    }); 
    }; 

    $.fn.jclock.getServerOffset = function(el) { 
    //Want to make a synchronous call to the server to get the server time. 
    $.ajax({ 
     url: "Time.php", 
     async: false, 
     context: el, 
     success: function(result) { 
      var serverDate = new Date(+(result) * 1000); //Convert the seconds to a number, and multiple by 1000 to get milliseconds. 
      var clientDate = new Date(); 

      $this = $(this.context[0]); 

      $this.serverOffset = clientDate - serverDate; //Set the offset between server and client. 
     } 
    }); 
    }; 

    $.fn.jclock.startClock = function(el) { 
    $.fn.jclock.stopClock(el); 
    $.fn.jclock.displayTime(el); 
    }; 

    $.fn.jclock.stopClock = function(el) { 
    if(el.running) { 
     clearTimeout(el.timerID); 
    } 
    el.running = false; 
    }; 

    $.fn.jclock.displayTime = function(el) { 
    var time = $.fn.jclock.getTime(el); 
    el.html(time); 
    el.timerID = setTimeout(function(){$.fn.jclock.displayTime(el)},1000); 
    }; 

    $.fn.jclock.getTime = function(el) { 
    var now = new Date(new Date().getTime() - el.serverOffset); //Apply the server offset. 
    var hours, minutes, seconds; 

    if(el.utc == true) { 
     if(el.utc_offset != 0) { 
     now.setUTCHours(now.getUTCHours()+el.utc_offset); 
     } 
     hours = now.getUTCHours(); 
     minutes = now.getUTCMinutes(); 
     seconds = now.getUTCSeconds(); 
    } else { 
     hours = now.getHours(); 
     minutes = now.getMinutes(); 
     seconds = now.getSeconds(); 
    } 

    var am_pm_text = ''; 
    (hours >= 12) ? am_pm_text = " P.M." : am_pm_text = " A.M."; 

    if (el.timeNotation == '12h') { 
     hours = ((hours > 12) ? hours - 12 : hours); 
    } else { 
     hours = ((hours < 10) ? "0" : "") + hours; 
    } 

    minutes = ((minutes < 10) ? "0" : "") + minutes; 
    seconds = ((seconds < 10) ? "0" : "") + seconds; 

    var timeNow = hours + ":" + minutes + ":" + seconds; 
    if ((el.timeNotation == '12h') && (el.am_pm == true)) { 
    timeNow += am_pm_text; 
    } 

    return timeNow; 
    }; 

    // plugin defaults 
    $.fn.jclock.defaults = { 
    timeNotation: '24h', 
    am_pm: false, 
    utc: false, 
    fontFamily: '', 
    fontSize: '', 
    foreground: '', 
    background: '', 
    utc_offset: 0 
    }; 

})(jQuery); 

如何在其上添加日期,所以它會顯示週一,週二和等?我現在的時間通過time.php通過

echo time(); 

我希望有人能夠幫助我,因爲我不想給碰壞獲得。 這是一個很大的提前感謝。

回答

1

JavaScript沒有打印出當天的名字像PHP做的一個很好的方式,您必須將姓名手工編碼,從週日:

var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; 
var date = new Date(); 
console.log(days[date.getDay()]); // Wednesday 

此處瞭解詳情:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay

編輯:如果你想用jclock插件完成這一切,你可以使用format參數來指定輸出格式。當天名稱的format符號是%A。有關使用format選項的示例,請參閱:https://github.com/dsparling/jclock/blob/master/examples/clock2.html

+1

您的邏輯是正確的,但是您應該在答案中引用更可靠和更新的文檔。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay –

+0

我真的很感激,如果它以某種方式可能包括它在我目前的代碼上面。我真的不想打破某些東西。 –

+0

@ФейсалСайед:謝謝,我更新了鏈接 – alecananian

相關問題