Reply LinkBack Thread Tools Search this Thread
Old 11-12-2007, 14:37   #1 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
automatic greeting php

Hey, how would i use php to say either good morning or night / evening with php?

cheers
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 11-12-2007, 14:46   #2 (permalink)
d3mcfadden
Senior Member
 
d3mcfadden's Avatar
 
Join Date: Apr 2005
Location: -
Posts: 694
Send a message via AIM to d3mcfadden
PHP Code:
switch (date("a"))
{
  case 
"am":
    echo 
"Good morning fuck face";
  break;

  case 
"pm":
   echo 
"Good evening asshole";
  break;

  Reply With Quote
Old 11-12-2007, 14:58   #3 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
just to complicate it, how would i get it to say those depending on the times, so i can have severl alike

-its early
-good morning
-good afternoon
-good evening
-its nearly midnight

you see what i mean?
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 11-12-2007, 15:02   #4 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,960
thanks.
__________________
  Reply With Quote
Old 11-12-2007, 15:12   #5 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
huh? why the thanks? are you saying i shud of said thanks, well thanks anyway lol didnt wanna seem rude. but still how would i do what i asked on my second reply
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 11-12-2007, 15:29   #6 (permalink)
Stealth
Registered User
 
Join Date: Dec 2007
Posts: 3

Get yourself familiar with PHP's date function. Look in up on php.net

date( "G" ) will give you the hour of the day. Write some if-elseif conditions to determine what phrase to say.

You can easily say different phrases every hour. Just create an array (for example, $greetings) with 24 strings (your phrases) in it:

$greetings = array(
"OMG, it's midnight!",
"Seems like you've got too much coffee",
"....."
);

then output the corresponding string with
echo( $greetings[ date( "G" ) ] );

Play with it! It's fun! :)
  Reply With Quote
Old 11-12-2007, 15:57   #7 (permalink)
Larixk
Senior Member
 
Larixk's Avatar
 
Join Date: Sep 2006
Location: Utrecht, Netherlands
Posts: 1,028
Send a message via MSN to Larixk
thanks
__________________
  Reply With Quote
Old 11-12-2007, 18:09   #8 (permalink)
iblastoff
gotsa a malanga!
 
iblastoff's Avatar
 
Join Date: Apr 2006
Location: ottawa, canada
Posts: 489
have you done any beginner tutorials for php?? all of them usually start with something as simple as this.
__________________
  Reply With Quote
Old 11-12-2007, 18:18   #9 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,605
The problem with this, is that it will decide if its morning or evening based on the time on the server, not the time where the viewer is. So if your server is in the states, and I am in Japan, then when I access your site in the evening, its going to say good morning.
  Reply With Quote
Old 11-12-2007, 18:59   #10 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
is there anyway i can set it dependin on the viewer like some grab timezone script or something
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 11-12-2007, 19:20   #11 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,340
Quote:
Originally Posted by mgpwr
is there anyway i can set it dependin on the viewer like some grab timezone script or something
I suppose you could trace their IP, but that's not necessarily accurate. Wouldn't know how to do it, either.

Personally, I think it's not worth the effort.
  Reply With Quote
Old 11-12-2007, 20:20   #12 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,605
I wrote this script a few weeks back and posted it on another forum. Add it to the top of your php script

Code:
<?php if(!isset($_COOKIE['offset'])) { ?> <script type="text/javascript"> { var now = new Date(); var client_offset = now.getTimezoneOffset() * 60; document.cookie = 'offset=' + client_offset; window.location=("<?php echo curPageName(); ?>"); } </script> <?php } ?>

PHP checks to see if there is a cookie set telling the user's timezone offset. If there isn't, it outputs some javascript that sets a cookie with the users timezone offset, then reloads the page. PHP can then use that cookie to know the users timezone offset. Add (or is it subtract?) this offset from the server timezone, and you will get the current time for the user. Then use your php script to check that timezone and output the applicable greeting depending on the time of day.
  Reply With Quote
Old 12-12-2007, 09:00   #13 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
Hey, the script you just posted, won't work, all that happens is the script is wrote into the text and not classed as a php script????

Also, i know you said its basic, but how would i set it to display a mesage based on the time, i dnt understand what the last person wrote about the array! i know its basic but meh, i am basic lol

Cheers
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 12-12-2007, 09:09   #14 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,605
Sorry, I just realized I referenced a script that I didn't include in my post. Try this:

Code:
<?php function curPageName() { return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1); } if(!isset($_COOKIE['offset'])) { ?> <script type="text/javascript"> { var now = new Date(); var client_offset = now.getTimezoneOffset() * 60; document.cookie = 'offset=' + client_offset; window.location=("<?php echo curPageName(); ?>"); } </script> <?php } ?>
  Reply With Quote
Old 12-12-2007, 09:17   #15 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,605
Truth be told though, if you can't understand the array function that was explained, I don't think my script is going to help you at all, as I'm guessing you won't know what to do with the cookie that is set.
  Reply With Quote
Old 12-12-2007, 10:12   #16 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,340
I don't like the way you tab your braces in haku.
  Reply With Quote
Old 12-12-2007, 10:38   #17 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,605
Ah sorry about that! haha
I know its not the way anyone else does it except me, I just find it easier to scan through the DOM that way to see what belongs to what. In the past I have remembered to standardize it before pasting it into the forum here, but I didn't do it this time.
  Reply With Quote
Old 13-12-2007, 07:27   #18 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
Its not that i don't understand the array function, i just cnt see how i cud use that depending on the time, for example when i tried this out before asking for help on here. i used

<
$date = date('H:i');
$Morning = "00:00";

if ($date > $Morning)
{
echo 'Good Morning';
}

etc.. for all the variables like morning, evening and night. However i found that when it passed dinnertime the output wud be "Good Morning Good Afternoon" because it works on the principle of it been passed the time so technically it is true what its saying, i need to work it out thats all - any help?
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Old 13-12-2007, 10:38   #19 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,605
I'll elaborate on what Stealth said, because his script was quite good.

start by setting an array:

Code:
$message = array("midnight message", "1am message", "2am message", "3am message");

And fill it up all the way to 11pm in the same manner. This means that that $message[0] is the midnight message, $message[1] is the 1am message, $message[2] is the 2am message and so on.

Now look at this code:

Code:
date("G");

This code will output a number between 0-23 depending on the time of day. It will output '0' between midnite and 12:59, output '1' between 1 and 1:59, output '2' between 2 and 2:59, and so on up till '23' between 11pm and 23:59.

So finally, the code to put it all together
Code:
echo $message(date("G"));

Since date("g") outputs a number between 0-23, and the array $message is filled with values between $message[0] and $message[23], the function will first evaluate the date function, returning a number between 1-23 to the $message array, causing the $message array to return the equivalent message for that time of day. The echo command then outputs it to the browser.

This is all good, except that the date() function will return the time based on your server, so you will have to adjust the server time to match the user's time using the cookie that is set in the code I gave you earlier. So what you actually have to do is find out the current time using time(), adjust that to GMT, then use the cookie that is set using the code I gave earlier in this thread to adjust the current GMT to the users time. Then use:
Code:
echo $greeting($date("G",$current_time_in_users_time_zone))
  Reply With Quote
Old 13-12-2007, 11:51   #20 (permalink)
mgpwr
with a hint of lemon
 
mgpwr's Avatar
 
Join Date: Aug 2006
Location: Sheffield
Posts: 489
when i use this script:

<?

$message = array("midnight message", "1am message", "2am message", "3am message","4","5","6","7","8","9","10","11","12","1 3","14","15","16","17","18","19","21","22","23" );

echo $message(date("G"));

?>

i get this error:
Fatal error: Call to undefined function: array() in mgpwr.co.uk/greeting.php on line 5
__________________
Online portfolio: mgpwr.co.uk
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8