| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
with a hint of lemon
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
|
|
|
|
|
|
#3 (permalink) |
|
with a hint of lemon
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
|
|
|
|
#5 (permalink) |
|
with a hint of lemon
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
|
|
|
|
#6 (permalink) |
|
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! :) |
|
|
|
#9 (permalink) |
|
shiro
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. |
|
|
|
#10 (permalink) |
|
with a hint of lemon
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
|
|
|
|
#11 (permalink) | |
|
Senior Member
Join Date: Jan 2005
Posts: 12,340
|
Quote:
Personally, I think it's not worth the effort. |
|
|
|
|
#12 (permalink) |
|
shiro
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 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. |
|
|
|
#13 (permalink) |
|
with a hint of lemon
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
|
|
|
|
#14 (permalink) |
|
shiro
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:
|
|
|
|
#15 (permalink) |
|
shiro
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. |
|
|
|
#17 (permalink) |
|
shiro
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. |
|
|
|
#18 (permalink) |
|
with a hint of lemon
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
|
|
|
|
#19 (permalink) |
|
shiro
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:
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:
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:
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:
|
|
|
|
#20 (permalink) |
|
with a hint of lemon
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
|
|
![]() |