Jump to content

How to write DATE script into a Function ??


spacepoet

Recommended Posts

Hello:

I have this snippet of code that will write the current year onto a page if it's embedded on the page itself:

<?php echo date("Y") ?>

 

Works fine.

 

But, when I include it in an included file and pull it onto the page with a function, it does not work.

 

Like this:

MyNav.php

<?php

function spFooter() {
$spFooter =
"
   <p>
   
   © <?php echo date(\"Y\") ?>

   </p>

";

return $spFooter;
}
?>

 

MyPage.php

<?php
include('include/myNav.php');
?>
<html>
...

<?php echo spFooter(); ?>

...
</html>

 

What is the proper way to do this, and also what are the basic rules for properly nesting PHP scripts like this?

 

Thanks for the help.

I'd probably do something more like this:

 

function spFooter() 
{
$spFooter ="<p> ©";
$spFooter .= date("Y");
$spFooter .= "</p>";
return $spFooter;
}

 

First, you're including <?php tags while you're in a php script, and you're echo'ing within a variable? Your way of doing it would be this:

function spFooter() {
$spFooter =
"
   <p>
   
   © ". date('Y')."

   </p>

";

return $spFooter;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.