pouncer Posted November 18, 2006 Share Posted November 18, 2006 how can i convert this php function to java scrpt[code=php:0] function GetGreeting() { if (date("H") < 12) return "Good morning!"; elseif (date("H") < 16) return "Good afternoon!"; else return "Good evening!"; }[/code]it displays the greeting depending on the time on the server as php is server-side.i want to fix it to display greeting depending on the time on the users machine - javascript.any help please? Link to comment https://forums.phpfreaks.com/topic/27683-greeting-javascript/ Share on other sites More sharing options...
heckenschutze Posted November 18, 2006 Share Posted November 18, 2006 well I'm pretty crap at javascript, but here it is:[code]<script language="Javascript">function GetGreeting(){ var objDate = new Date(); if(objDate.getHours() < 12) return "Good morning"; else if(objDate.getHours() < 16) return "Good afternoon"; return "Good evening"; }</script>[/code]and to test:[code]<script language="Javascript">document.write(GetGreeting());</script>[/code]hth Link to comment https://forums.phpfreaks.com/topic/27683-greeting-javascript/#findComment-126623 Share on other sites More sharing options...
pouncer Posted November 18, 2006 Author Share Posted November 18, 2006 thanks but how can i make it return the greeting in php.because i use like$log->GetGreeting();its a function in my class Link to comment https://forums.phpfreaks.com/topic/27683-greeting-javascript/#findComment-126628 Share on other sites More sharing options...
jsladek Posted November 18, 2006 Share Posted November 18, 2006 This makes no sense you want to conver a PHP function to a JavaScript function and then have the javascript function return PHP? Is that right?-John Link to comment https://forums.phpfreaks.com/topic/27683-greeting-javascript/#findComment-126649 Share on other sites More sharing options...
Barand Posted November 18, 2006 Share Posted November 18, 2006 try[code]<?phpif (isset($_POST['action'])) { if ($_POST['usertime'] < 12) $greeting = 'Good morning'; elseif ($_POST['usertime'] < 16) $greeting = 'Good afternoon'; else $greeting = 'Good evening'; echo $greeting . ' ' . $_POST['name'];}?><html><head><meta name="generator" content="PhpED Version 4.5 (Build 4513)"><title>Sample</title><meta name="author" content="Barand"><script language='javascript'> function formsubmit() { var objDate = new Date(); document.myform.usertime.value = objDate.getHours() return true; } </script></head><body><form method='POST' name = 'myform' onsubmit='return formsubmit()'> Name <input type="text" name="name" size="20"><br/> <input type="hidden" name="usertime"> <input type="submit" name="action" value="Submit"></form></body></html>[/code] Link to comment https://forums.phpfreaks.com/topic/27683-greeting-javascript/#findComment-126777 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.