2014-05-25 77 views
3

您好,我目前正在学习PHP的最新版本,我正在尝试创建一个脚本来执行以下操作。捕获一个人加入该网站的日期和时间;从加入日期和时间计算一年;然后,在每个新的一天过去之后,计算在到达未来日期之前的剩余天数。
这是我迄今为止PHP日期倒计时从加入日期到未来一年

<?php 
//This top half s working just fine 
$datejoin = new DateTime(); 
echo $datejoin->format('D M j h:i:s A Y') .'<br>'; 

$dateoneyear = new DateTime(); 
$dateoneyear->add(new DateInterval('P12M')); 
echo $dateoneyear->format('D M j h:i:s A Y') . '<br>'; 
'<br>'; 
$datetime1 = new DateTime(); 
$datetime2 = new DateTime(); 
$datetime2-> add(new DateInterval('P12M')); 
$interval = $datetime1->diff($datetime2); 
echo $interval->format('%R%a days remaining'); 
?> 

我有被捕获与计算的日期值这一问题上的“$ dateoneyear->添加(新DateInterval(‘P12M’));”,然后使用子方法来倒计时。
当然,我可能会接近这一切错误,但这是我能够一起拉开。我一直在使用php.net/manual/en/来弄清楚如何使用sub命令,但所有使用的例子都已经插入了日期,例如03-25-2009。
由于我的日期是动态的,因此我需要从创建的日历中获取未来值并倒计时,但我似乎无法找到实现该日期的方法。

回答

1

所需的差异是“现在”和“到期”日期之间的天数。 到期日期为给定“加入”日期后的12个月。

剩余天数为:

$dateJoin = new DateTime('2014/01/01'); 
$dateExpire = $dateJoin->add(new DateInterval('P12M')); 
$now = new DateTime(); 
$remaining = $now->diff($dateExpire); 

$daysRemaining = $remaining->format('%R%a'); 
echo 'days remaining :', $daysRemaining; 
+0

花了一点点找出逻辑,因为手动结果从显示的结果不同。不过,我终于明白,原来的日期是12个月的原因是因为 非常感谢@RyanVincent;我非常感谢您及早回复我的问题。计算完美! – cyberfool

+0

@cyberfoot,欢迎您。 –