Jump to content

[SOLVED] How to insert specific time of the day in a page. Eg: Good Morning.


iconicCreator

Recommended Posts

I have managed to create a script from reading and practicing, the script changes or display random images every time the page is reloaded or the browser is refreshed.

 

The codes are in an include folder because I like to separate certain PHP script from HTML, this way I can have some stuff in separate places for easier trouble shooting.

 

My question is or should I say, I need the caption that display along with the images to also display the time of the day.

 

Example: Good morning Bob, if the person is visiting the page in the morning. Or Good afternoon Bob, if someone is visiting the page in the afternoon and so on.

 

This is what I have so far.

 

<?php
$images = array(
  array('file'    => 'image1',
        'caption' => 'Good morning, <br /> my name is Rick, how may I help you?'),
  array('file'    => 'image2',
        'caption' => 'Good morning, <br /> my name is Dianna, how may I help you'),
  array('file'    => 'image3',
        'caption' => 'Good morning, <br /> my name is David, how may I help you'),
  array('file'    => 'image4',
        'caption' => 'Good morning, <br /> my name is Mary, how may I help you'),
  );
$i = rand(0, count($images)-1);
$selectedImage = "images/{$images[$i]['file']}.png";
$caption = $images[$i]['caption'];
?>

 

The script above is saved in an include folder.

 

This is the HTML portion:

 

<?php include('includes/random_image.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="assets/journey.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>

<img src="<?php echo $selectedImage; ?>" alt="Random image" />
<p class="caption"><?php echo $caption; ?></p>
          

</body>
</html>

 

How do I get this going?

Where do I start? What I want to do is new to me, I am still a novice!

 

Any ideas or direction?

 

Thanks everyone!

 

IC

 

 

Link to comment
Share on other sites

Well, if it was up to me, first I'd replace word "morning" in $images array with something like "[PARTOFDAY]".

Then, I'd create a function, which, according to time, replaces [PARTOFDAY] with the corresponding part.

 

it would look something like that:

 

function PartOfDay($string) {

$hour = date("H",time());

if($hour >= 6 && $hour < 12) $replace = 'morning';

elseif($hour >= 12 && $hour < 18) $replace = 'afternoon';

elseif($hour >= 18 && $hour < 24) $replace = 'evening';

else $replace = 'god knows what';

return str_replace("[PARTOFDAY]",$replace,$string);

}

 

and, to make this work, you'll just write <?php echo PartOfDay($caption); ?>

 

hope I got this right, good luck ;)

Link to comment
Share on other sites

Well, if it was up to me, first I'd replace word "morning" in $images array with something like "[PARTOFDAY]".

Then, I'd create a function, which, according to time, replaces [PARTOFDAY] with the corresponding part.

 

it would look something like that:

 

function PartOfDay($string) {

$hour = date("H",time());

if($hour >= 6 && $hour < 12) $replace = 'morning';

elseif($hour >= 12 && $hour < 18) $replace = 'afternoon';

elseif($hour >= 18 && $hour < 24) $replace = 'evening';

else $replace = 'god knows what';

return str_replace("[PARTOFDAY]",$replace,$string);

}

 

and, to make this work, you'll just write <?php echo PartOfDay($caption); ?>

 

hope I got this right, good luck ;)

 

Hey thank you so much for the time and the effort!

I have not worked with the code yet but I will try this and let you know how things come out.

 

Your effort is all that counts and I really appreciate it!

 

Thanks again - will get back to you!

 

IC

Link to comment
Share on other sites

Hey there! Just wanted to get back to you on this.

It works! Yeah! I had to do little changes in terms of making some corrections with the code you showed me, there were inconsistency with some of the naming. Eg: PARTOFDAY and PartOfDay, not the same.

 

Anyway, it works perfectly!

 

You've been a very great help!

 

Have a very wonderful day!

 

IC

 

 

Link to comment
Share on other sites

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.