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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.