2016-10-22 135 views
-2

我想检查今年的事件是否在未来或以后。 所以我建立这个,它的工作原理。带日期('Y')功能的日期时间

$date = new DateTime('2016-04-07'); 
$now = new DateTime(); 

if($date > $now) { 
    echo 'future'; 
} else { 
    echo 'past'; 
} 

但我不想改变,每年的“2016”,以保持它在明年的工作。 有没有可能为2016添加实际的年份?

喜欢的东西:

$date = new DateTime('".date('Y')."-04-07'); 

感谢

+3

你实际上将'date('Y')'作为字符串传递而不是函数,因为它在引号内。 'new DateTime(date('Y')。“ - 04-07”)'应该是你要找的。 – Qirel

+0

什么是活动日期?这是从哪里来的?当然,这肯定是你从某个地方得到的变量,所以不会总是有2016年?会吗? – RiggsFolly

+0

*“试试这个”*氏族在下面为你发布了一些东西; “试试”。 –

回答

1

试试这个,

$date = new DateTime(date('Y').'-04-07'); 
1

你可以用这个尝试:

$date = date("Y")."-04-07"; 
$date = new DateTime($date); 

或者干脆:

$date = new DateTime(date("Y")."-04-07");