Jump to content

Recommended Posts

[code]
function GetGreeting() {
return '<script>
var objDate = new Date();
if (objDate.getHours() < 12) document.write("Good morning");
elseif (objDate.getHours() < 16) document.write("Good afternoon");
else document.write("Good evening");
</script>';
}
[/code]

It's a function inside my php class, but when i do $log->GetGreeting() it just doesn't work.
If I use this

[code]
function GetGreeting() {
?>
<script>
document.write("dfsdfsd");
</script>
<?php
}
[/code]

then $log-GetGreeting() puts  dfsdfsd on the page - fine.

But this doesnt work:

[code]
function GetGreeting() {
?>
<script>
var objDate = new Date();
if (objDate.getHours() < 12) document.write("Good morning");
elseif (objDate.getHours() < 16) document.write("Good afternoon");
else document.write("Good evening");
</script>
<?php
}
[/code]
Then I think the problem is with your javascript code. View the source, if everything is printed to the html page, then it worked. If your HTML page contains this:
<script>
var objDate = new Date();
if (objDate.getHours() < 12) document.write("Good morning");
elseif (objDate.getHours() < 16) document.write("Good afternoon");
else document.write("Good evening");
</script>

Then the problem is NOT the PHP, but the JS. Does JS have an elseif construct?
Why are you doing this in Javascript, when it can done in PHP just as easily:
[code]<?php
if (date('G') < 12) echo 'Good Morning';
elseif (date('G') < 16) echo 'Good Afternoon';
else echo 'Good Evening';
?>[/code]

Ken
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.