我想在一个网站的底部贴一张版权声明,但我认为这一年要过时太俗气了。如何使用php 4和php 5自动更新年份?
你可以使用任何数据或函数。在这个案例,我说它是无关紧要的一年是一年中的什么物质,(除非有一个最佳的现场,differently格式?)
例如:
1
| <?php echo date("Y"); ?> |
对注,当它在PHP格式的日期格式的问题时,你想你的时间比你在其他区域设置中的默认值。如果你使用的操作系统,和函数的函数。根据最新的PHP手册:
To format dates in other languages,
you should use the setlocale() and
strftime() functions instead of
date().
从这一点来看,我认为这将是最好使用尽可能多的函数,如果你甚至有一个可能性,你有到远程定位的应用。如果这是不是一个问题,你喜欢的最佳选择。
1
| <?php echo date("Y"); ?> |
我的超级懒惰版的地图显示的版权,这stays:自动更新
1 2 3 4 5
| © <?php
$copyYear = 2008;
$curYear = date('Y');
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?> Me, Inc. |
今年(2008年),它会说:
© 2008 Me, Inc.
下一年,它会说:
© 2008-2009 Me, Inc.
永远保持和更新与当前的一年。
(5.3.0 +或PHP)的紧凑的方式是使用匿名函数的一种操作系统,你有一个leaking输出变量和不重复的代码/常数:
1 2 3
| ©
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?>
Me, Inc. |
PHP面向对象朝更多的方向,我在这里引用《有没有人surprised DateTime类:内置
1 2
| $now = new DateTime();
$year = $now->format("Y"); |
一个衬板类成员或与访问在线instantiation(PHP > = 5.4):
1
| $year = (new DateTime)->format("Y"); |
us2.php.net http:/ / /日期
我爱函数。这是伟大的功能块(recombining抓/日期/时间。
加上这方面的功能的设置,该设置是不是最新的。
这一个给你当地的时间:
1
| $year = date('Y'); // 2008 |
与这一个UTC(协调世界时):
1
| $year = gmdate('Y'); // 2008 |
4位数字表示法:
1
| <?php echo date('Y'); ?> |
2位数字表示:
1
| <?php echo date('Y'); ?> |
检查PHP文档的更多信息:http:////EN / function.date.php secure.php.net手册
这是我的:
1
| <?php echo date("d-m-Y") ?> |
下面是一位美国的解释:什么什么学院
1 2 3
| d = day
m = month
Y = year |
将给你四位数的Y和Y的(例如,1990)的两位数字(如90)
你echo date('Y')岁获得电流,这将自动更新为当前的日期,因为date()吧。
更多信息,检查文件的日期()函数:https:////EN / function.date.php secure.php.net手册
只是写:
1 2
| date("Y") // A full numeric representation of a year, 4 digits
// Examples: 1999 or 2003 |
或:
1
| date("y"); // A two digit representation of a year Examples: 99 or 03 |
"这和‘回波值…………………
这是一个用PHP函数,就称为date()。
它以当前的日期,你提供给它的格式
和格式是Y Y是资本就要去一年是四位数。
1
| <?php echo date("Y"); ?> |
1
| <?php echo date("Y"); ?> |
这个代码应该的
如果你的服务器支持PHP,你使用短标签,或5.4,你可以使用:
如果PHP 5.4升到
1 2 3 4 5 6 7
| <?php
$current= new \DateTime();
$future = new \DateTime('+ 1 years');
echo $current->format('Y');
//For 4 digit ('Y') for 2 digit ('y')
?> |
你可以使用它,或与一个线
1
| $year = (new DateTime)->format("Y"); |
如果你想增加或减少的一年一个样线法;添加修改如下。
1 2 3 4 5 6
| <?PHP
$now = new DateTime;
$now->modify('-1 years'); //or +1 or +5 years
echo $now->format('Y');
//and here again For 4 digit ('Y') for 2 digit ('y')
?> |
你可以使用简单的PHP Date类。它提供了许多有用的方法和功能:
1 2 3 4 5 6
| $date = new simpleDate();
echo $date->now()->getYear();
echo $date->now()->getMonth();
echo $date->set('2013-01-21')->getDayString();
echo $date->now()->compare('2013-01-21')->isBefore();
... |
你可以检查库页更多的实例教程
顺便说一句……有一些方法,如何正确显示的网站的版权。有一些人做的事情的倾向,即:redundant版权所有?有两个相同的意义。重要的是:《版权所有配件
1
| **Symbol, Year, Author/Owner and Rights statement.** |
Using PHP + HTML:
1 2
| <p id='copyright'>© <?php echo date("Y"); ?> Company Name All Rights Reserved
</p> |
或
1
| <p id='copyright'>© <?php echo"2010-".date("Y"); ?> Company Name All Rights Reserved</p |
date()使用PHP函数。
和格式是Y Y是资本就要去一年是四位数。
1
| <?php echo date("Y"); ?> |
1
| <?php date_default_timezone_set("Asia/Kolkata");?><?=date("Y");?> |
你可以使用这个在页脚部分,以获得动态年版权所有
年:全用get
1 2 3
| <?php
echo $curr_year = date('Y'); // it will display full year ex. 2017
?> |
有两位年仅(或使用像这样:
1 2 3
| <?php
echo $curr_year = date('y'); // it will display short 2 digit year ex. 17
?> |
我的方式显示出版权,这使自动在线更新
1 2 3 4 5 6 7 8
| <p class="text-muted credit">Copyright ©
<?php
$copyYear = 2017; // Set your website start date
$curYear = date('Y'); // Keeps the second year updated
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?>
</p> |
它将输出的结果
1 2
| copyright @ 2017 //if $copyYear is 2017
copyright @ 2017-201x //if $copyYear is not equal to Current Year. |
这是最好的部分:shortcode
如果你是使用PHP的API扩展碳是可怕的,你可以轻松的实现它的日期:
<?php echo Carbon::now()->year; ?>
的欢呼声。
1 2 3 4 5 6
| <?php
$time_now=mktime(date('h')+5,date('i')+30,date('s'));
$dateTime = date('d_m_Y h:i:s A',$time_now);
echo $dateTime;
?> |