Pregunta

Here's my copyright as of right now:

<i>Example.com</i> &copy; 2013 - <?php echo date('Y'); ?>

this is okay next year because it will read:

Example.com © 2013 - 2014

but this year it says: © 2013-2013

How can I make it © 2013 and auto switch to © 2013-2014 next year, without having to come back and change it by hand?

¿Fue útil?

Solución

It should be

<i>Example.com</i> &copy; 2013 <?php (date('Y') !== "2013") echo "- " . date('Y')); ?>

Otros consejos

$startYear = 2013; 
$currentYear = date('Y');
echo $startYear;
if ($startYear != $currentYear) {
    echo '-' . $currentYear;
}

That handles the years part of the Copyright message, just enter the other formatting around the output as you require.

(I've aimed for the longer but more readable approach, take your pick)

You can use the php code to get dynamic year as well as the domain of your hosting server

<?php echo "&copy; ". date(Y)." ".$_SERVER['HTTP_HOST'] ;?>
<i>Example.com</i> &copy; 2013 <?=(date('Y')>2013?' - '.date('Y'):'')?>
<?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 } ?>
//where you want to use just paste it:
<?php auto_copyright('2010'); ?>//2010-2015
<?php auto_copyright(); ?>//current year i.e:2015
<i>Example.com</i> &copy; <?php echo (date('Y')==2018)?(date('Y')):'2018 - '.date('Y'); ?>
<i>Example.com</i> &copy; <?php if (date('Y') != '2013') { echo '2013 - ' } echo date('Y'); ?>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top