gwolff2005 Posted April 11, 2009 Share Posted April 11, 2009 Hi guys, I need your help. I have a script which shows the time on my server, but does not realize the clients timezones. How can I greet my customers according to THEIR timezone? print date("g:ia"); $date = date ("H"); if ($date < 6) { echo "Good Morning "; } elseif ($date < 7) { echo "Good Morning"; } elseif ($date < 12) { echo "Good Morning "; } elseif ($date < 18) { echo "Good Afternoon "; } elseif ($date < 22) { echo "Good Evening "; } elseif ($date < 24) { echo "Good Evening"; } ?> Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 I'm not sure if it's possible to change the time zone depending on the client's location in PHP. You could do it in javascript though. It'll automatically display the time on the client's computer. Quote Link to comment Share on other sites More sharing options...
gwolff2005 Posted April 11, 2009 Author Share Posted April 11, 2009 Hi jackpf, thanks for the fast reply. Do you have any idea how to program this in java, due to my non knowledge of this language ??? Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 11, 2009 Share Posted April 11, 2009 I actually have written a function specifically for this myself Check out my site here : http://www.jackpf.co.uk/beta.php?action=profile&status=profile&profile=index&alias=Admin Look at the source code; you can nick my function if you so desire. Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 11, 2009 Share Posted April 11, 2009 ask them most common. untested support at url . http://stackoverflow.com/questions/13/determining-web-users-time-zone function ajaxpage(){ var url = "timezone.php"; var visitortime = new Date(); vat time = visitortime.getTimezoneOffset()/60; var page_request = false if (window.XMLHttpRequest) page_request = new XMLHttpRequest() else if (window.ActiveXObject){ try { page_request = new ActiveXObject("Msxml2.XMLHTTP") } catch (e){ try{ page_request = new ActiveXObject("Microsoft.XMLHTTP") } catch (e){}}} else return false page_request.onreadystatechange=function(){ loadpage(page_request, containerid) } if (bustcachevar) bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime() page_request.open('GET', url+bustcacheparameter+"&time="+time, true) page_request.send(null) } function loadpage(page_request, containerid){ if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) document.write('<meta http-equiv="refresh" content="0;url=http://example.com/"/>'); } <?php session_start(); $_SESSION['time'] = $_GET['time']; ?> Quote Link to comment Share on other sites More sharing options...
gwolff2005 Posted April 11, 2009 Author Share Posted April 11, 2009 Thanks you both! I will try that first thing tomorrow morning! Iw ill let you know! Quote Link to comment Share on other sites More sharing options...
gwolff2005 Posted April 13, 2009 Author Share Posted April 13, 2009 @Jackpf, I tried your java script but it does not appear on my screen. @redarrow, the scruipt does not work. What can I do????? ??? Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 You do need to put this somewhere on your page: <span id="clock"></span> as well as the javascript. It obviously does work because it's working on my site Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 13, 2009 Share Posted April 13, 2009 you could use geoip to locate them and map that to a timezone however this is labour intensive. if you have users that login perhaps ask them to select which timezone they reside in (save it in their profile) and use that to amend your base time ins any computations/display. you could also set a cookie for guest visitors - a discreet part of the page could request their timezone or a nasty javascript pop-up (even a nicely-still nasty for UI styled ajax pop-up). Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Lol, this all seems a bit excessive Especially considering I have produced a small amount of javascript that does it anyway! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 13, 2009 Share Posted April 13, 2009 what if javascript is off? admittedly i doubt this is a critical task - but i am always of the ilk that your site should work - not be dependant on any technologies that may or may not be available. in this case yeah I'll concede the js option is probably right. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 13, 2009 Share Posted April 13, 2009 If you are expecting to personalize a site to your visitor's timezone, you should provide a way for the visitor to set his GMT offset/time zone (saved in your user table per visitor or even as a cookie that would get sent with each page request.) You can offer to autodetect it using some javascript, but that should not be the primary method. Quote Link to comment Share on other sites More sharing options...
gwolff2005 Posted April 13, 2009 Author Share Posted April 13, 2009 Hi, I integrated now the code exactly like this into my php document <script type="text/javascript">/*<![CDATA[*/ function clock() { ////////////////////////////////////////////////////// //Coded by the notorious jackpf ///////////////////////////////////////////////////// //generate time var time = new Date(); var hours = time.getHours(); var minutes = time.getMinutes(); var seconds = time.getSeconds(); //add preceding 0s, if required var hours = (hours < 10 ? '0' : '')+hours; var minutes = (minutes < 10 ? '0' : '')+minutes; var seconds = (seconds < 10 ? '0' : '')+seconds; //generate formated time var time = hours+':'+minutes+':'+seconds; //display time document.getElementById('clock').innerHTML = time; } //init clock window.onload = function() { clock(); setInterval('clock()', 1000); } /*]]>*/ </script> and I entered <span id="clock"></span> what do I have to write now to make it visible for the users to see either good morning/afternoon/evening depending on THEIR time? Thanks so much! Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 When the page has finished loading, <span id="clock"></span> should contain the current time. Quote Link to comment Share on other sites More sharing options...
gwolff2005 Posted April 13, 2009 Author Share Posted April 13, 2009 Hi Jackpf, thanks for the reply. I wrote that (see my last post) but still it does not show the time.. When I watch your site I cannot see the time either... I am lost.... Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 There!!! [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
gwolff2005 Posted April 13, 2009 Author Share Posted April 13, 2009 Hi Jackpf, I attach the photo of your webpage as I see it and as you can see it does not display the timne. Apart from that. How can I display, the good morning, good evening, etc. depending on the time... [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Ahh, I had some dodgy if IE stuff, I didn't realise you were using IE. It shoudl work now. Quote Link to comment Share on other sites More sharing options...
gwolff2005 Posted April 13, 2009 Author Share Posted April 13, 2009 Hey, you rock! What did you do? What do I have to do, to get it done. So, when I have the time? whta do I have to write in java ( i don't know the language and in php I am a newbie) to show the visitors, good morning, good afternoon or good evening, depending on their time??? I really appreciate your help. I owe you something! Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 <script type="text/javascript"> /*<![CDATA[*/ function clock() { ////////////////////////////////////////////////////// //Coded by the notorious jackpf ///////////////////////////////////////////////////// //generate time var time = new Date(); var hours = time.getHours(); var minutes = time.getMinutes(); var seconds = time.getSeconds(); //add preceding 0s, if required var hours = (hours < 10 ? '0' : '')+hours; var minutes = (minutes < 10 ? '0' : '')+minutes; var seconds = (seconds < 10 ? '0' : '')+seconds; //generate formated time var time = hours+':'+minutes+':'+seconds; //get where abouts in the day it is if(hours >= 0 && hours < 12) { var greeting = 'Good morning'; } else { var greeting = 'Good evening'; } //display time document.getElementById('clock').innerHTML = time+'<br />'+greeting; } //init clock window.onload = function() { clock(); setInterval('clock()', 1000); } /*]]>*/ </script> <span id="clock"></span> Somethin glike that? You can add more greetings in if you want, but that's the basics. And no problem. Quote Link to comment Share on other sites More sharing options...
gwolff2005 Posted April 13, 2009 Author Share Posted April 13, 2009 Hi Jackpf I got this message Warning: Cannot modify header information - headers already sent by (output started at /home/guntmar/public_html/intro.php:35) in /home/guntmar/public_html/intro.php on line 77 This is the code right now <?php //initialize the session if (!isset($_SESSION)) { session_start(); } // ** Logout the current user. ** $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true"; if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){ $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){ //to fully log out a visitor we need to clear the session varialbles $_SESSION['MM_Username'] = NULL; $_SESSION['MM_UserGroup'] = NULL; $_SESSION['PrevUrl'] = NULL; unset($_SESSION['MM_Username']); unset($_SESSION['MM_UserGroup']); unset($_SESSION['PrevUrl']); $logoutGoTo = "http://www.guntmarwolff.com"; if ($logoutGoTo) { header("Location: $logoutGoTo"); exit; } } ?> <?php require_once('Connections/Login.php'); ?><?php //initialize the session if (!isset($_SESSION)) { session_start(); } ?> <span id="clock"></span> <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = ""; $MM_donotCheckaccess = "true"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && true) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "index.html"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = $_SERVER['PHP_SELF']; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) $MM_referrer .= "?" . $QUERY_STRING; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Intro</title> <style type="text/css"> <!-- .style3 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #000033; } .style8 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #000033; font-weight: bold; } .style11 {color: #000033} --> </style> </head> <body> <p> </p> <table width="917" border="0"> <tr> <td height="32" colspan="2" class="style8"><span class="style8"> <?php echo " " . $_SESSION['MM_firstname']; ?></span></td> <td width="71" class="style8"> </td> <td width="107" class="style8"> </td> <td class="style3"> </td> <td width="164" class="style3"><div align="left"><span class="style3"><?php echo "You are logged in as<br>"?><em><strong><?php echo $_SESSION['MM_Username']; ?></strong></em></span></div></td> <td width="163" class="style3"><span class="style8"><a href="<?php echo $logoutAction ?>"><img src="pics/logout.jpg" width="86" height="24" border="0" longdesc="http://www.guntmarwolff.com" /></a></span></td> </tr> <tr> <td colspan="2" class="style3">script type="text/javascript"> /*<![CDATA[*/ function clock() { ////////////////////////////////////////////////////// //Coded by the notorious jackpf ///////////////////////////////////////////////////// //generate time var time = new Date(); var hours = time.getHours(); var minutes = time.getMinutes(); var seconds = time.getSeconds(); //add preceding 0s, if required var hours = (hours < 10 ? '0' : '')+hours; var minutes = (minutes < 10 ? '0' : '')+minutes; var seconds = (seconds < 10 ? '0' : '')+seconds; //generate formated time var time = hours+':'+minutes+':'+seconds; //get where abouts in the day it is if(hours >= 0 && hours < 12) { var greeting = 'Good morning'; } else { var greeting = 'Good evening'; } //display time document.getElementById('clock').innerHTML = time+'<br />'+greeting; } //init clock window.onload = function() { clock(); setInterval('clock()', 1000); } /*]]>*/ </script> <span id="clock"></span> </td> <td colspan="2"> </td> <td width="125"> </td> <td valign="top"> </td> <td class="style3"><span class="style11"> <?php //This file shows you how to include the file in your php document. include("online.php"); ?> </span></td> </tr> <tr> <td width="95"> </td> <td width="162"> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td colspan="2" rowspan="15"> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> <td> </td> <td> </td> <td> </td> </tr> </table> <p> </p> <p> </p> <p> <a href="<?php echo $logoutAction ?>"></a></p> </body> </html> Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Yeah, you're trying to redirect the user with a header, after you've displayed the clock. Why are you trying to redirect them? Then they won't see the wonderful clock Quote Link to comment Share on other sites More sharing options...
gwolff2005 Posted April 13, 2009 Author Share Posted April 13, 2009 Hi Jackpf, you are amazing now it works!!! I had the clock twice in tehre. Now it works... Thanks so much!!!!!!!!!!!!!!!!!!!!!!!!! You rock!!!! Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 I know Lol no problem Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.