2014-09-01 43 views
1

我在计算年度年份条件时遇到了麻烦。如何检查年度日期条件

在我的项目中,我们允许用户随时创建一个计划,但他们的结束日期将会修复,即(3月 - 31日)。 用户每年只能创建一个计划。在明年再次他们可以创建一个新的计划,同样的方式将在未来所有年。

现在我的问题是如何检查这个条件,用户已经为今年创造了计划?

我正在计划创建日期(从MySQL)的阵列如下:

Array 
(
    [0] => 2010-04-19 08:20:45 
    [1] => 2011-09-19 08:20:45 
    [2] => 2012-05-19 08:20:45 
    [3] => 2013-08-19 08:20:45 
) 

我没有得到实现这一权利的方式。

+2

'$ prevYear = SUBSTR($阵列[3],4);如果($ prevYear ==日期('Y')){' – Daan 2014-09-01 10:24:41

+0

你想检查数组中已经存在的值吗? – 2014-09-01 10:24:45

+0

@punithasubramaniv是的 – 2014-09-01 10:25:42

回答

1

这个怎么样?

$last = strtotime("2014-04-01"); 

    $years_diff = (int) date("Y") - (int) date("Y", $last); 

    if($years_diff == 1){ 
     if(date('Y-m-d') > date('Y-m-d', strtotime((date('Y',$last)+1)."-03-31"))) 
     { 
     echo 'Let them create new plan'; exit; 
     } 
    } 
    else if($years_diff == 0){ 
    if(date('m',$last) > 03){ 
     echo 'Sorry ! you can not create'; exit; 
    } 
    if(date('Y-m-d') > date('Y-m-d', strtotime(date('Y',$last)."-03-31"))) 
    { 
     echo 'Let them create new plan'; exit; 
    } 
    } 
    else 
     echo 'Let them create new plan'; exit; 

希望它可以帮助你:)

+1

令人敬畏的兄弟。像魅力:) – 2014-09-01 11:50:34

+0

快乐编码:) – Kalpit 2014-09-01 12:00:18

1

尝试#1

第一种方法是最好的:你比较两个日期年(在你的情况 - 日期时间)。本年必须比最后日期的年份更大:2010 - 2014年那你比较相同,但有一些更精确的日,月:条件

$current = time(); 
$last = strtotime("2010-04-19 08:20:45"); 
$years_diff = (int) date("Y", $current) - (int) date("Y", $last); 
$months_diff = (int) date("n", $current) - (int) date("n", $last); 
$days_diff = (int) date("j", $current) - (int) date("j", $last); 

$condition = false; 
if ($years_diff == 1) { // ABOUT one year ago when last plan was created 
    if ($months_diff == 0) { // Current month is month when plan was created 
     if ($days_diff >= 0) { // Larger than or equal to day when last plan was created 
      $condition = true; 
     } 
    } 
    else if ($months_diff > 0) { // Next months in new year 
     $condition = true; 
    } 
} 
else if ($years_diff > 1) { // ABOUT 2 or more years ago when last plan was created 
    $condition = true; 
} 

还有的压缩版本:

$condition = (($years_diff == 1) ? (($months_diff == 0) ? ($days_diff >= 0) : ($months_diff > 0)) : ($years_diff > 1)); 



尝试#2

简单的方法是计算拉斯之间的差t和当前日期(在你的情况下 - “datetime”)。然后检查差异是否大于或等于365天。

$current = time(); 
$last = strtotime("2010-04-19 08:20:45"); 
$days_diff = ($current - $last)/(60 * 60 * 24); 
$condition = ($days_diff >= 365); 
+0

感谢解决方案兄弟:) – 2014-09-01 11:51:11