Jump to content

greeting - javascript?


pouncer

Recommended Posts

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

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

try
[code]
<?php
if (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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.