View Full Version : Time dependant greeting
pswindle
08-21-2008, 12:05 PM
Hello,
Is there a simple way of having my site greet people differently in the morning and afternoon?
For example if the time is before midday it will display one .jpg that says good morning. In the afternoon it will display an alternative.
Any thoughts?
I'd suggest either PHP or javascript.
What you would to with php to make this work is to have one variable that will have PM || AM depending on the time. If that variable is AM, make another variable have an image location, and if it is PM otherwhise, then put this variable in an image tag in HTML. Below is a PHP example.
<?php
$timeOfDay = date("A");
if($timeOfDay == "AM"){
$imageDisplay = "http://www.dynamicdrive.com/forums/designfiles/logo.gif"; //morning.gif
} else {
$imageDisplay = "http://www.dynamicdrive.com/ddincludes/logo.gif";//evening.gif
}
echo "<img src='$imageDisplay' />";
?>
pswindle
08-22-2008, 04:17 PM
Hi thank for the reply.
php is a mystery to me. I thought it was used in conjunction with MySQL.
How would accomplish the same thing with java?
Or am I wrong about php being complex?
Thanks a lot
boogyman
08-22-2008, 05:17 PM
java or javascript? they are very very different in terms of programming language.
PHP is not as complex as some of the other server-side languages, however if you have no experience in programming or website creation, the methodologies can be confusing when you first start.
If you were talking about javascript, you can use the following code.
Put this in your <head> tag.
<script type="text/javascript">
function displayGreeting() {
var today = new Date();
var hr = today.getHours();
if(hr > 12) {
document.write('Good Morning'); // morning midnight - 12am
} else if(hr > 18) {
document.write('Good Afternoon'); // afternoon 12-6pm
} else {
document.write('Good Evening'); // evening 6pm - midnight
}
}
</script>
put the following whereever you wish to have the script display
<script type="text/javascript">displayGreeting()</script>
yes, as Boogyman said, PHP is not complex at all, well at least the basics.
Boogyman, I don't know if he wanted it to be Morning, Afternoon, and Evening.
Pswindle, if you only wanted afternoon and evening use this:
<script type="text/javascript">
function displayGreeting() {
var today = new Date();
var hr = today.getHours();
if(hr > 12) {
document.write('Good Morning'); // morning midnight - 12am
} else {
document.write('Good Evening'); // evening 6pm - midnight
}
}
</script>
pswindle
08-23-2008, 02:17 PM
Thanks both.
I'd found out the document.write way of doing it with javascript (not java as you rightly point out.) but was looking for a way to display a different .jpg according to the time of day.
Thanks again.
pswindle
08-23-2008, 02:40 PM
Thanks I've got it.
Here it is.
<script type="text/javascript">
function displayGreeting() {
var today = new Date();
var hr = today.getHours();
if(hr > 12) {
document.writeln("<img src=\"goodafternoon.jpg\" /img /p>");
} else if(hr > 18) {
document.writeln("<img src=\"goodevening.jpg\" /img /p>");
} else {
document.writeln("<img src=\"goodmorning.jpg\" /img /p>");
}
}
</script>
pswindle
08-23-2008, 06:51 PM
Sorry that didn't quite work. This does though.
Thanks again:
<script type="text/javascript">
function displayGreeting() {
var today = new Date();
var hr = today.getHours();
if(hr > 12 && hr < 18) {
document.writeln("<img src=\"goodafternoon.jpg\" /img /p>");
} else if(hr > 18) {
document.writeln("<img src=\"goodevening.jpg\" /img /p>");
} else {
document.writeln("<img src=\"goodmorning.jpg\" /img /p>");
}
}
</script>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.