[英]How to get previous year in php?
I've a format date like this 我有这样的格式化日期
$y = date('Y');
so the output is 2015
所以输出是2015
but if I try getting previous year using the below code: 但是如果我尝试使用以下代码获取上一年的信息:
$yr = date('Y'($y,'-1 year'));
echo $yr;
I get incorrect output. 我得到不正确的输出。 The output is 1970
. 输出为1970
。
How to get the previous year? 如何获得前一年?
You aren't using any library which does natural language processing
. 您没有使用任何进行natural language processing
库。 A better option will be to do 更好的选择是
$y = date('Y') - 1;
Also, I am worried about the syntax: date('Y'($y...
另外,我担心语法: date('Y'($y...
EDIT 编辑
Exactly as I expected, your code doesn't even compile: Parse error: syntax error, unexpected '(' in /scratchpad/index.php on line 3
完全符合我的预期,您的代码甚至无法编译: Parse error: syntax error, unexpected '(' in /scratchpad/index.php on line 3
Try creating a function which does this: 尝试创建执行此操作的函数:
function getYear( $before = 0 ) {
return ( date( 'Y' ) - $before );
}
Even better is to use strtotime
: 更好的是使用strtotime
:
function getYear( $context ) {
return date( 'Y', strtotime( $context ) );
}
Then use it something like: 然后使用类似:
echo ( getYear( '-1 year' ) );
will give: 2014
. 将给出: 2014
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.