Jump to content

Recommended Posts

Hi there, can someone help to write a code for Copyright tag at the footer of the page. The format needs to be webpage start year - current year (eg © 2015-2020 ). I need to use function and parameters. The code should be as simple as possible as i am just a beginer! Thanks

Hi, I should have posted what I have so far. So I had three versions of solving this, they are below:

<?= '&copy 2015 -' . date("Y");?>
<?php function auto_copyright($year = 'auto'){ 
    if(intval($year) == 'auto'){ $year = date('Y'); }
    if(intval($year) < date('Y')){ echo '&copy ' . intval($year) . ' - ' . date('Y'); } 
    if(intval($year) > date('Y')){ echo '&copy ' . date('Y'); } 
 } auto_copyright(); 
auto_copyright('2016'); ?>

 

<?php
    $startDate = 2015; 
    $currentDate = date('Y'); 
      echo '&copy ' . $startDate . (($startDate != $currentDate) ? '-' . $currentDate : '');  ?>

What I am looking for is another way to solve this, particularly involving function. 

Cheers

  • Like 1

Seems you are overthinking/ developing this.  The first year is a parameter you will be passing.  You don't need any fancy math.

If you want to consider possible problems with the parameter, these come to mind:

  1. what if string parameter doesn't equate to a valid year?
  2. what if string parameter is missing or empty?
  3. what if string parameter is in the future?
  4. what if the string parameter is a year equal to the current year?

Here's a solution that handles all these possibilities:

<?php

function getCopyrightRange($startYear) {
  $currentYear = date('Y');
  
  if (((int)$startYear == 0) || ($startYear > $currentYear)) {
      $startYear = $currentYear;
  }
  return ($startYear == $currentYear) ? "&copy;$startYear" : "&copy;$startYear - $currentYear";
}

echo getCopyrightRange('1985') . PHP_EOL;
echo getCopyrightRange('2025') . PHP_EOL;
echo getCopyrightRange('') . PHP_EOL;
echo getCopyrightRange('2020') . PHP_EOL;

The tests are setup just for command line php testing, so obviously since this is intended to be html markup using an htmlentity, you wouldn't want or need that.

Here's the results:

&copy;1985 - 2020
&copy;2020
&copy;2020
&copy;2020

If you were writing some test cases, you'd likely have a case for each of these potential issues, assuming you were concerned about them.

1 hour ago, gizmola said:

Seems you are overthinking/ developing this.  The first year is a parameter you will be passing.  You don't need any fancy math.

If you want to consider possible problems with the parameter, these come to mind:

  1. what if string parameter doesn't equate to a valid year?
  2. what if string parameter is missing or empty?
  3. what if string parameter is in the future?
  4. what if the string parameter is a year equal to the current year?

Here's a solution that handles all these possibilities:


<?php

function getCopyrightRange($startYear) {
  $currentYear = date('Y');
  
  if (((int)$startYear == 0) || ($startYear > $currentYear)) {
      $startYear = $currentYear;
  }
  return ($startYear == $currentYear) ? "&copy;$startYear" : "&copy;$startYear - $currentYear";
}

echo getCopyrightRange('1985') . PHP_EOL;
echo getCopyrightRange('2025') . PHP_EOL;
echo getCopyrightRange('') . PHP_EOL;
echo getCopyrightRange('2020') . PHP_EOL;

The tests are setup just for command line php testing, so obviously since this is intended to be html markup using an htmlentity, you wouldn't want or need that.

Here's the results:


&copy;1985 - 2020
&copy;2020
&copy;2020
&copy;2020

If you were writing some test cases, you'd likely have a case for each of these potential issues, assuming you were concerned about them

Thank you for your help. I am taking fast track web development classes, and this was one of the homework tasks - I gave my tutor all three previous answers, which still return the correct outcome, but he insisted it must be done with a function and parameter.

55 minutes ago, saaima said:

Thank you for your help. I am taking fast track web development classes, and this was one of the homework tasks - I gave my tutor all three previous answers, which still return the correct outcome, but he insisted it must be done with a function and parameter.

So you got us to write the code for you. Well done.

It won't happen again.

I have been trying my best, and needed extra help - I thought this is what forums are for, when you have no one else to ask for help. It wasn’t intended to be malicious or abusive and warning was completely uncalled for.

Edited by saaima

I'm sure it was not malicious, but most people don't appreciate it when someone has homework and uses forums like ours to get the answers.

Next time, please remind us that you're learning and let us know that your tutor asked you to do something. That way we can help you get to the right answer without accidentally writing all the code for you.

  • Like 1

Yes - forums (including this one) DO help people.  But not with all of their tasks.  Forums like this one help you address your code.  Help you correct it or debug it or help you amend your solution.  We are not here to just plain write it for you.   We are here for people who are having difficulty but that means showing us the difficulty.

  • Like 1
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.