Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: How to show server time in java script code?

  1. #1
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to show server time in java script code?

    I have JavaScript code and each city time should change evary minut but not with clients PC time but with server time with PHP. Do you have any suggestions?

    Code for time is:


    HTML Code:
    <!-- ############################## -->
    <!-- ##### Start of HTML Code ##### -->
    
    <table border="0" cellspacing="2" cellpadding="0" class="worldTimeTable" style="border-style: solid; border-color: #A0A0A0; border-width: 1px;">
    	<tr>
    		<td id="FrankfurtDT" class="worldTimeTable"></td>
    		<td width="10"></td>
    		<td id="TokyoDT" class="worldTimeTable"></td>
    	</tr>
    </table>
    <!-- ##### End of HTML Code ##### -->
    <!-- ############################ -->
    
    
    <!-- #################################### -->
    <!-- ##### Start of JavaScript Code ##### -->
    
    <script language="JavaScript">
    var timerID ;
    function tzone(tz, os, ds, cl)
    {
    	this.ct = new Date(0) ;		// datetime
    	this.tz = tz ;		// code
    	this.os = os ;		// GMT offset
    	this.ds = ds ;		// has daylight savings
    	this.cl = cl ;		// font color
    }
    
    function UpdateClocks()
    {
    	var ct = new Array(
    
    		new tzone('<b>Frankfurt:</b> ',  1, 1, ''),
    		new tzone('<b>Tokyo:</b> ',  +9, 0, '')
    	) ;
    
    	var dt = new Date() ;	// [GMT] time according to machine clock
    	var startDST = new Date(dt.getFullYear(), 3, 1) ;
    	while (startDST.getDay() != 0)
    		startDST.setDate(startDST.getDate() + 1) ;
    
    	var endDST = new Date(dt.getFullYear(), 9, 31) ;
    	while (endDST.getDay() != 0)
    		endDST.setDate(endDST.getDate() - 1) ;
    
    	var ds_active ;		// DS currently active
    	if (startDST < dt && dt < endDST)
    		ds_active = 1 ;
    	else
    		ds_active = 0 ;
    
    	// Adjust each clock offset if that clock has DS and in DS.
    	for(n=0 ; n<ct.length ; n++)
    		if (ct[n].ds == 1 && ds_active == 1) ct[n].os++ ;
    
    	// compensate time zones
    	gmdt = new Date() ;
    	for (n=0 ; n<ct.length ; n++)
    		ct[n].ct = new Date(gmdt.getTime() + ct[n].os * 3600 * 1000) ;
    
    
    	document.getElementById("FrankfurtDT").innerHTML = ct[4].tz + ClockString(ct[4].ct);
    	document.getElementById("TokyoDT").innerHTML = ct[5].tz + ClockString(ct[5].ct);
    
    	timerID = window.setTimeout("UpdateClocks()", 60000) ;
    }
    
    function ClockString(dt)
    {
    	var stemp, ampm ;
    	var dt_year = dt.getUTCFullYear() ;
    	var dt_month = dt.getUTCMonth() + 1 ;
    	var dt_day = dt.getUTCDate() ;
    	var dt_hour = dt.getUTCHours() ;
    	var dt_minute = dt.getUTCMinutes() ;
    	dt_year = dt_year.toString() ;
    
    	if (dt_minute < 10)
    		dt_minute = '0' + dt_minute ;
    	if (dt_hour < 10)
    		dt_hour = '0' + dt_hour ;
    
    	stemp = getMonthText(dt_month) + ' ' + dt_day + ' ' ;
    	stemp = stemp + ' ' + dt_hour + ":" + dt_minute ;
    	return stemp ;
    }
    
    function getMonthText(tempMon)
    {
    	switch (tempMon)
    	{
    		case 1: return "Jan"; break;
    		case 2: return "Feb"; break;
    		case 3: return "Mar"; break;
    		case 4: return "Apr"; break;
    		case 5: return "May"; break;
    		case 6: return "Jun"; break;
    		case 7: return "Jul"; break;
    		case 8: return "Aug"; break;
    		case 9: return "Sep"; break;
    		case 10: return "Oct"; break;
    		case 11: return "Nov"; break;
    		case 12: return "Dec"; break;
    	}
    }
    UpdateClocks();
    </script>
    <!-- ##### End of JavaScript Code ##### -->

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    	var dt = new Date() ;	// time according to machine clock
      dt.setHours(<?php echo(date("G")); ?>);
      dt.setMinutes(<?php echo(date("i")); ?>);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have put:

    Code:
     var dt = new Date() ;	// time according to machine clock
        dt.setHours(<?php echo(date("G")); ?>);
        dt.setMinutes(<?php echo(date("i")); ?>);
    	var stemp, ampm ;
    	var dt_year = dt.getUTCFullYear() ;
    	var dt_month = dt.getUTCMonth() + 1 ;
    	var dt_day = dt.getUTCDate() ;
    	var dt_hour = dt.getUTCHours() ;
    	var dt_minute = dt.getUTCMinutes() ;
    	dt_year = dt_year.toString() ;
    1. Question: Do you know why it shows me always the same time
    Can be changed each minut?
    and
    2. Qestion: it is in all cities the same value. what is wrong?

    I hope you have some suggestions

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Have you looked into:

    Local Time script

    Description: Want to display the current local time of a specific place in the world? This DHTML script, with the help of either PHP, ASP, or SSI (server side includes), displays the current server time your site is hosted on, and in turn, the local time of anywhere on Earth. You can use it to show visitors your current local time, or the time in New York, Tokyo etc. Now, in order to use this script, the webpage you're adding it to must be either PHP (ie: .php), ASP, or SSI (ie: .shtml) enabled. More on this below
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    this is very good. I have tested, but do not know if I can change view of time?
    I would like to have it 16:15

    So, time should be (without AM or PM and date):
    HOUR:MINUTES

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This line:
    Code:
    this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"
    Change to:
    Code:
    this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Change to:
    Code:
    this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)
    It works, but time have to be SHORT

    Thanks

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I have no idea what you're talking about.
    The script doesn't work unless the time is short?
    You want the time to be output in a "short" form? If so, how exactly do you want it to be shorter than hh:mm format?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I would like to show time in format: HH:MM, format should be 24 h and without AM or PM and date.

    I have changed code to this as you told me:
    PHP Code:
    showLocalTime.prototype.updateContainer=function(){
    var 
    thisobj=this
    if (this.displayversion=="long")
    this.container.innerHTML=this.localtime.toLocaleString()
    else{
    var 
    hour=this.localtime.getHours()
    var 
    minutes=this.localtime.getMinutes()
    var 
    seconds=this.localtime.getSeconds()
    var 
    ampm=(hour>=12)? "PM" "AM"
    var dayofweek=weekdaystxt[this.localtime.getDay()]
    this.container.innerHTML=formatField(hour1)+":"+formatField(minutes
    and to display (tokyo time):
    new showLocalTime("timecontainer7", "server-php", 540, "long") //GMT +9

    What to do that it wil show tokyo time like Hours:Minutes (24 hour format)

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Aha. Find:
    Code:
    if (typeof isHour!="undefined"){ //if this is the hour field
    var hour=(num>12)? num-12 : num
    return (hour==0)? 12 : hour
    }
    Replace with:
    Code:
    if (typeof isHour!="undefined")
      return (hour < 10 ? "0" + hour : hour);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •