Search code examples
phpcopyright-display

how to make automatic copyright webpage footer in php using a function


I want a code or function that will show copyright from starting year to current year.

Example

© 2012-2013
© 1998-2013

Solution

  • Current year only

    &copy; <?php echo date("Y") ?>
    

    With start year

    &copy; 2008-<?php echo date("Y") ?>
    

    Start date with error protection

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

    Usage:

    <?php auto_copyright(); // 2011?>
    
    <?php auto_copyright('2010');  // 2010 - 2011 ?>