473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help using localtime()

Can anyone help me out on how to get this time dependent code to work. I
want it to display a greeting depending on the hour of the day (I'm trying
to convert my own Perl code to PHP with no luck on this one). I want it to
use the time of the local computer rather than the server.

Thanks
Dariusz
<?PHP

$greeting = array("Good morning", "Good afternoon", "Good evening");

$LTime = localtime(tm_hour);

if($LTime >= "0" && $LTime < "12")
{
$greet = $greeting[0]; # If morning print greeting Good Morning
}
elseif($LTime >= "12" && $LTime < "18")
{
$greet = $greeting[1]; # If 12 - 5 PM print greeting Good Afternoon
}
elseif($LTime >= "18" && $LTime < "23")
{
$greet = $greeting[2]; # If 6 - 11 PM print greeting Good Evening
}

echo "$greet";

?>
Jul 17 '05 #1
4 2142
Dariusz wrote:
Can anyone help me out on how to get this time dependent code to work. I
want it to display a greeting depending on the hour of the day (I'm trying
to convert my own Perl code to PHP with no luck on this one). I want it to
use the time of the local computer rather than the server.
No can do. Unless the local computer sends the time to the server, the
server has no way to know it.
<?PHP

$greeting = array("Good morning", "Good afternoon", "Good evening");

$LTime = localtime(tm_hour);
see http://www.php.net/localtime

$LTime is now an array with information about time on the server

if($LTime >= "0" && $LTime < "12")
This comparison is turned into
if ("Array" >= "0" && "Array" < "12")

{
$greet = $greeting[0]; # If morning print greeting Good Morning
}
elseif($LTime >= "12" && $LTime < "18")
{
$greet = $greeting[1]; # If 12 - 5 PM print greeting Good Afternoon
}
elseif($LTime >= "18" && $LTime < "23")
{
$greet = $greeting[2]; # If 6 - 11 PM print greeting Good Evening
}
What happens between 23:00:00 and 23:59:59 ? :-)

echo "$greet";

?>

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2
In article <c3*************@ID-203069.news.uni-berlin.de>, Pedro Graca <he****@hotpop.com> wrote:
<<snip>>

Thanks Pedro for your sugestions and comments, I will try them out.

Dariusz
Jul 17 '05 #3
Dariusz wrote:

Can anyone help me out on how to get this time dependent code to work. I
want it to display a greeting depending on the hour of the day (I'm trying
to convert my own Perl code to PHP with no luck on this one). I want it to
use the time of the local computer rather than the server.

How do you think it would get such information?

Brian Rodenborn
Jul 17 '05 #4
Use client-side code instead.

<html>
<body>
<script>

now = new Date();
hours = now.getHours();

if(hours < 12) {
greeting = 'Good morning';
}
else if(hours < 18) {
greeting = 'Good afternoon';
}
else {
greeting = 'Good evening';
}

document.write(greeting);

</script>
<noscript>Time to die!</noscript>
</body>
</html>

Uzytkownik "Dariusz" <ng@lycaus.plusYOURSHIT.com> napisal w wiadomosci
news:9h*********************@wards.force9.net...
Can anyone help me out on how to get this time dependent code to work. I
want it to display a greeting depending on the hour of the day (I'm trying
to convert my own Perl code to PHP with no luck on this one). I want it to
use the time of the local computer rather than the server.

Thanks
Dariusz
<?PHP

$greeting = array("Good morning", "Good afternoon", "Good evening");

$LTime = localtime(tm_hour);

if($LTime >= "0" && $LTime < "12")
{
$greet = $greeting[0]; # If morning print greeting Good Morning
}
elseif($LTime >= "12" && $LTime < "18")
{
$greet = $greeting[1]; # If 12 - 5 PM print greeting Good Afternoon
}
elseif($LTime >= "18" && $LTime < "23")
{
$greet = $greeting[2]; # If 6 - 11 PM print greeting Good Evening
}

echo "$greet";

?>

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: NSC | last post by:
Hi all, I have this : <SNIP> foreach $item (@NEW_FILES) print "\n $item \n ";
2
by: Raskolnikow | last post by:
Hi! I have a very simple problem with itoa() or the localtime(...). Sorry, if it is too simple, I don't have a proper example. Please have a look at the comments. struct tm *systime; time_t...
5
by: Raj | last post by:
Hi I'm very new to C and am trying to get the time a month ago in the format mmyy. What I have tried so far is: char *last_mth; size_t maxsize = 4; struct tm *timeptr = localtime(time(0));
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
16
by: maruk2 | last post by:
I have some old data files with old timestamps, where timestmap=time(NULL), some of them date back to the year 1999. I want to my code to print the timestamps and each one to include...
1
by: maruk2 | last post by:
I have some old data files with old timestamps, where timestmap=time(NULL), some of them date back to the year 1999. I want my code to print the timestamps and each one to include...
2
by: vj | last post by:
I have a perl script which connect to network stream using sockets. The scripts first logins in to the server and then parses the data comming from the socket. Statement 1: my $today =...
2
by: jonathan184 | last post by:
Hi basically what i want this script to do is In a particular dir there are alot of files from today back to 1999 or earlier. So I am trying to have the script search this dir and sort the files...
1
by: nigelesquire | last post by:
I am in need of your assistance. I have a page with multiple unixtime stamps and I need to display them in readable time and date with the user's time zone. Below is the code. <!DOCTYPE...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.