paulmo Posted December 18, 2008 Share Posted December 18, 2008 I need to print statements based on user's time (server/ip), and this is close but it's still is off: $time = intval(date('G')) < 12 ? 'morning' : intval(date('G')) < 17 ? 'afternoon' : 'evening'; advice please? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/ Share on other sites More sharing options...
Adam Posted December 18, 2008 Share Posted December 18, 2008 Can you have a shorthand if..else if..else ? Try: $time = (intval(date('G')) < 12) ? 'morning' : (intval(date('G')) < 17) ? 'afternoon' : 'evening'; A Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-718731 Share on other sites More sharing options...
paulmo Posted December 18, 2008 Author Share Posted December 18, 2008 to clarify, the code works but time is off. will try the ), thanks. Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-718738 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 Do you know the user's time? If not you may have to use Javascript to get the user's time and display that, cause PHP cannot tell you what the user's time is unless you have them fill out their timezone and then change the time accordingly... Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-718741 Share on other sites More sharing options...
paulmo Posted December 18, 2008 Author Share Posted December 18, 2008 tried your ()'s, code works but time's still off. (explain please why xtra () ? i'm a noob. how to use js or jquery to get user time? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-718749 Share on other sites More sharing options...
Adam Posted December 18, 2008 Share Posted December 18, 2008 For JS date: http://www.w3schools.com/jS/js_obj_date.asp Would appear you can use if...else if... else shorthand notations! If anyone else was wondering.. A Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-718753 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 <script type="text/javascript"> function GetClientUTC() { var now = new Date(); var offset = now.getTimezoneOffset(); return offset; } </script> Unsure if that works, but yea, you could use that and post the offset to your script on user login and use that via session to change the offset. Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-718754 Share on other sites More sharing options...
paulmo Posted December 18, 2008 Author Share Posted December 18, 2008 how to echo my $time keywords (morning, afternoon evening) with your js, premiso? there's no user log in on my page/site. and this from mradam's link also seems relevant (but again, don't know how to integrate): <script type="text/javascript"> var d = new Date(); document.write (d.toLocaleTimeString()); </script> thanks for help. Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-719110 Share on other sites More sharing options...
paulmo Posted December 19, 2008 Author Share Posted December 19, 2008 bump, how to integrate js with my previous code that echo statements based on time? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-719952 Share on other sites More sharing options...
paulmo Posted December 28, 2008 Author Share Posted December 28, 2008 getting a parse error on line 61 "<script type=" which looks fine to me. trying to get user local time to work right with the morning afternoon evening statement. help please? and thanks: <script type="text/javascript"> function.GetClientUTC() { var now = new Date(); var offset = now.getTimezoneOffset(); return offset; } </script> $time = (GetClientUTC() < 12) ? 'morning' : (intval(date('G')) < 17) ? 'afternoon' : 'evening'; Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-724890 Share on other sites More sharing options...
premiso Posted December 28, 2008 Share Posted December 28, 2008 Well yea it will not work. You are mixing JS and PHP. They are two different languages JS has to be done on the client side. So in order for this to work you will have to setup a page that loads before anything else is loaded and set the locale probably as a cookie using javascript. Then take it to the original page and access the locale from the cookie. Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-724892 Share on other sites More sharing options...
paulmo Posted December 28, 2008 Author Share Posted December 28, 2008 thanks, possible to do the time variable with javascript and no php? i'd still need to echo the time according to morning afternoon evening in the php code: switch($_POST['theme']) { case 'grey': echo "A gloomy {$phrase1} {$time} blankets your spirit, Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-724893 Share on other sites More sharing options...
MadTechie Posted December 28, 2008 Share Posted December 28, 2008 Using cookies works well <script type="text/javascript"> var myDate = new Date(); var myTime = myDate.toLocaleTimeString(); myDate.setDate(myDate.getDate()+1); setCookie('UserTime',myTime); function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()); } </script> <?php list($hours, $minutes, $seconds) = explode(":",$_COOKIE['UserTime']); echo "Its $hours:$minutes:$seconds"; $time = ($hours < 12)?'morning'($hours < 17)?'afternoon':'evening'); echo $time; ?> Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-724929 Share on other sites More sharing options...
paulmo Posted December 29, 2008 Author Share Posted December 29, 2008 madtechie, thanks for the cookie code. it's working. i'm trying to comprehend it vs. the php GetClientUTC. Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-725281 Share on other sites More sharing options...
paulmo Posted December 30, 2008 Author Share Posted December 30, 2008 alas, the $time statements are not matching the clock time. for example, just now i'm getting The time is 7:28:30 PM Good morning here's the code. thanks for help! <script type="text/javascript"> var myDate = new Date(); var myTime = myDate.toLocaleTimeString(); myDate.setDate(myDate.getDate()+1); setCookie('UserTime',myTime); function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()); } </script> <script type="text/javascript" src="jquery-1.2.6.min.js"></script> <script type="text/javascript" src="jquery.corners.min.js"></script> <div id="col1"> <div style="background-color:#FFFFFF; padding:10px" class="rounded {10px}"> <?php list($hours, $minutes, $seconds) = explode(":",$_COOKIE['UserTime']); echo "The time is $hours:$minutes:$seconds"; $time = ($hours < 12)?'morning'($hours < 17)?'afternoon':'evening'); echo " Good $time"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-725817 Share on other sites More sharing options...
paulmo Posted December 30, 2008 Author Share Posted December 30, 2008 this code seems to work with the cookie: $time = intval(date('G')) < 12 ? 'morning' : intval(date('G')) < 17 ? 'afternoon' : 'evening'; how to echo/print $time and .date("l") on same line? thanks echo " Good $time"; echo "Today is ".date("l"); Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-725836 Share on other sites More sharing options...
paulmo Posted December 30, 2008 Author Share Posted December 30, 2008 actually the time of day [morning afternoon evening] is still not matching up with time, which is accurate. i've tried both $time statements: <?php list($hours, $minutes, $seconds) = explode(":",$_COOKIE['UserTime']); echo "The time is $hours:$minutes"; [correct] //$time = ($hours < 12)?'morning'($hours < 17)?'afternoon':'evening'); $time = intval(date('G')) < 12 ? 'morning' : intval(date('G')) < 17 ? 'afternoon' : 'evening'; [not] echo " on this ".date('l') ; [correct] echo " {$time}.";[not] Quote Link to comment https://forums.phpfreaks.com/topic/137532-user-serverip-time/#findComment-726135 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.