saaima Posted March 27, 2020 Share Posted March 27, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/ Share on other sites More sharing options...
gizmola Posted March 27, 2020 Share Posted March 27, 2020 This isn't a "write some code for me for free site", it's a "here is what I tried, can you help me with my code" site. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1575935 Share on other sites More sharing options...
ginerjm Posted March 27, 2020 Share Posted March 27, 2020 Are you going to register this copyright too? Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1575936 Share on other sites More sharing options...
ohno Posted March 28, 2020 Share Posted March 28, 2020 © 2010-<?php echo date("Y");?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1575953 Share on other sites More sharing options...
saaima Posted March 28, 2020 Author Share Posted March 28, 2020 Hi, I should have posted what I have so far. So I had three versions of solving this, they are below: <?= '© 2015 -' . date("Y");?> <?php function auto_copyright($year = 'auto'){ if(intval($year) == 'auto'){ $year = date('Y'); } if(intval($year) < date('Y')){ echo '© ' . intval($year) . ' - ' . date('Y'); } if(intval($year) > date('Y')){ echo '© ' . date('Y'); } } auto_copyright(); auto_copyright('2016'); ?> <?php $startDate = 2015; $currentDate = date('Y'); echo '© ' . $startDate . (($startDate != $currentDate) ? '-' . $currentDate : ''); ?> What I am looking for is another way to solve this, particularly involving function. Cheers 1 Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1575997 Share on other sites More sharing options...
saaima Posted March 28, 2020 Author Share Posted March 28, 2020 20 hours ago, ginerjm said: Are you going to register this copyright too? Hi there, no, it's just for training purposes, to make dates change automatically as time passes. Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1575999 Share on other sites More sharing options...
gizmola Posted March 28, 2020 Share Posted March 28, 2020 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: what if string parameter doesn't equate to a valid year? what if string parameter is missing or empty? what if string parameter is in the future? 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) ? "©$startYear" : "©$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: ©1985 - 2020 ©2020 ©2020 ©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. Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1576009 Share on other sites More sharing options...
saaima Posted March 29, 2020 Author Share Posted March 29, 2020 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: what if string parameter doesn't equate to a valid year? what if string parameter is missing or empty? what if string parameter is in the future? 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) ? "©$startYear" : "©$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: ©1985 - 2020 ©2020 ©2020 ©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. Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1576015 Share on other sites More sharing options...
requinix Posted March 29, 2020 Share Posted March 29, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1576022 Share on other sites More sharing options...
saaima Posted March 29, 2020 Author Share Posted March 29, 2020 (edited) 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 March 29, 2020 by saaima Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1576024 Share on other sites More sharing options...
requinix Posted March 29, 2020 Share Posted March 29, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1576025 Share on other sites More sharing options...
ginerjm Posted March 29, 2020 Share Posted March 29, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1576043 Share on other sites More sharing options...
gizmola Posted March 30, 2020 Share Posted March 30, 2020 To be fair to Saaima, I did not provide any code until posting of the attempts. I am sure the message was delivered as to what we expect in the future. Quote Link to comment https://forums.phpfreaks.com/topic/310392-php-code-using-function-for-copyright-2015-2020/#findComment-1576070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.