Eri*_*kel 1052
您可以使用date或strftime.在这种情况下,我会说,无论什么年份都是一年都没关系(除非有一个地方以不同的方式格式化年份?)
例如:
<?php echo date("Y"); ?>
Run Code Online (Sandbox Code Playgroud)
另外,在PHP格式化日期时,如果要将日期格式设置为与默认语言不同的语言环境,则这很重要.如果是这样,您必须使用setlocale和strftime.根据日期的php手册:
要格式化其他语言的日期,您应该使用setlocale()和strftime()函数而不是date().
从这个角度来看,我认为最好尽可能多地使用strftime,如果你甚至有可能无法本地化你的应用程序.如果这不是问题,请选择您最喜欢的那个.
Dan*_*ian 511
<?php echo date("Y"); ?>
Run Code Online (Sandbox Code Playgroud)
gre*_*mac 202
我的超级懒人版本显示版权行,自动保持更新:
© <?php
$copyYear = 2008;
$curYear = date('Y');
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?> Me, Inc.
Run Code Online (Sandbox Code Playgroud)
今年(2008年),它会说:
©2008 Me,Inc.
明年,它会说:
©2008-2009 Me,Inc.
并永远保持与当年的更新.
或者(PHP 5.3.0+)使用匿名函数执行此操作的紧凑方法,因此您不会泄漏变量并且不重复代码/常量:
©
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?>
Me, Inc.
Run Code Online (Sandbox Code Playgroud)
Tho*_*ley 64
随着PHP朝着更加面向对象的方向发展,我很惊讶这里没有人引用内置DateTime类:
$now = new DateTime();
$year = $now->format("Y");
Run Code Online (Sandbox Code Playgroud)
或者在实例化时使用类成员访问的单行(php> = 5.4):
$year = (new DateTime)->format("Y");
Run Code Online (Sandbox Code Playgroud)
Mar*_*iek 28
strftime("%Y");
Run Code Online (Sandbox Code Playgroud)
我喜欢strftime.它是抓取/重新组合日期/时间块的一个很好的功能.
另外,它尊重日期功能不能执行的区域设置.
Ale*_*dev 14
这个给你当地时间:
$year = date('Y'); // 2008
Run Code Online (Sandbox Code Playgroud)
这一个UTC:
$year = gmdate('Y'); // 2008
Run Code Online (Sandbox Code Playgroud)
joa*_*16v 12
对于4位数字表示:
<?php echo date('Y'); ?>
Run Code Online (Sandbox Code Playgroud)
2位数字表示:
<?php echo date('y'); ?>
Run Code Online (Sandbox Code Playgroud)
查看php文档以获取更多信息:https: //secure.php.net/manual/en/function.date.php
Abd*_*mad 11
这是我做的:
<?php echo date("d-m-Y") ?>
Run Code Online (Sandbox Code Playgroud)
下面是它的作用的一些解释:
d = day
m = month
Y = year
Run Code Online (Sandbox Code Playgroud)
Y会给你四位数(例如1990)和y给两位数(例如90)
小智 9
print date('Y');
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看date()函数文档:https://secure.php.net/manual/en/function.date.php
<?php echo date("Y"); ?>
Run Code Online (Sandbox Code Playgroud)
这段代码应该这样做
小智 7
如果您的服务器支持短标签,或者您使用PHP 5.4,则可以使用:
<?=date("Y")?>
Run Code Online (Sandbox Code Playgroud)
写吧:
date("Y") // A full numeric representation of a year, 4 digits
// Examples: 1999 or 2003
Run Code Online (Sandbox Code Playgroud)
要么:
date("y"); // A two digit representation of a year Examples: 99 or 03
Run Code Online (Sandbox Code Playgroud)
并且"回应"这个价值......
使用刚刚调用的PHP函数date().
它需要当前日期,然后您提供一种格式
格式只是Y.资本Y将是一个四位数的年份.
<?php echo date("Y"); ?>
Run Code Online (Sandbox Code Playgroud)
顺便说一句...有一些正确的方法如何显示网站版权。有些人倾向于使事情变得多余,即:版权©具有相同的含义。重要的版权部分是:
**Symbol, Year, Author/Owner and Rights statement.**
Run Code Online (Sandbox Code Playgroud)
使用 PHP + HTML:
<p id='copyright'>© <?php echo date("Y"); ?> Company Name All Rights Reserved</p>
Run Code Online (Sandbox Code Playgroud)
或者
<p id='copyright'>© <?php echo "2010-".date("Y"); ?> Company Name All Rights Reserved</p
Run Code Online (Sandbox Code Playgroud)
使用 PHPdate()函数。
格式将是 Y。大写 Y 将是一个四位数的年份。
<?php echo date("Y"); ?>
Run Code Online (Sandbox Code Playgroud)
您可以使用简单的PHP日期类.它提供了许多有用的方法和功能:
$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();
...
Run Code Online (Sandbox Code Playgroud)
您可以查看库教程页面以获取更多示例
最高为PHP 5.4+
<?php
$current= new \DateTime();
$future = new \DateTime('+ 1 years');
echo $current->format('Y');
//For 4 digit ('Y') for 2 digit ('y')
?>
Run Code Online (Sandbox Code Playgroud)
或者您可以使用一行
$year = (new DateTime)->format("Y");
Run Code Online (Sandbox Code Playgroud)
如果你想增加或减少一年的另一种方法; 添加如下所示的修改行.
<?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')
?>
Run Code Online (Sandbox Code Playgroud)
获得全年使用:
<?php
echo $curr_year = date('Y'); // it will display full year ex. 2017
?>
Run Code Online (Sandbox Code Playgroud)
或者像这样只使用两位数的年份:
<?php
echo $curr_year = date('y'); // it will display short 2 digit year ex. 17
?>
Run Code Online (Sandbox Code Playgroud)
小智 5
我的版权显示方式,不断自动更新
<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>
Run Code Online (Sandbox Code Playgroud)
它将输出结果为
copyright @ 2017 //if $copyYear is 2017
copyright @ 2017-201x //if $copyYear is not equal to Current Year.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1049302 次 |
| 最近记录: |