如果日期是下周/下个月/明年

时间:2014-03-28 19:07:59

标签: php date

我想知道是否可以使用PHP来确定具体日期。

例如:

// Next week
$date->setDate(2014, 4, 3);

if($date->format('Y-m-d') == NEXT_WEEK){
// Do something
}elseif($date->format('Y-m-d') == NEXT_MONTH){
// DO something else
}elseif($date->format('Y-m-d') == NEXT_YEAR){
// Do something else
}

我理解我的代码无法正常工作,但我正在寻找可以实现类似功能的东西吗?

3 个答案:

答案 0 :(得分:3)

轻松使用strtotime()

<?
$date = new DateTime();
$date->setDate(2014, 5, 2);


if($date->format('U') >= strtotime("next year")){
  echo "next year";
}elseif($date->format('U') >= strtotime("next month")){
  echo "next month";
}elseif($date->format('U') >= strtotime("next week")){
  echo "next week";
}

?>

请注意,仅使用>=要求您检查年份的FIRST,然后是月份,然后是一周,请strtotime("next year") > strtotime("next month") > strtotime("next week")

答案 1 :(得分:2)

这可以通过使用diff的DateTime对象来实现,以查看介于两者之间的时间,这样的事情可以解决这个问题:

// The date we want to check
$checkDate = new DateTime('2014-04-03', new DateTimeZone('UTC'));
// We need to compare it against today + a specific interval,
// so create a seperate DateTime object for today
$today = new DateTime('now', new DateTimeZone('UTC'));

// Check the difference between the dates
$diff = $today->diff($checkDate);

if ($diff->y == 0 && $diff->m == 0 && $diff->d == 7) {
    // Date lies a week in the future
} elseif ($diff->y == 0 && $diff->m == 1 && $diff->d == 0) {
    // Date lies a month in the future
} elseif ($diff->y == 1 && $diff->m == 0 && $diff->d == 0) {
    // Date lies a year in the future
}

答案 2 :(得分:1)

基于&#34;下周&#34;的各种定义,&#34;下个月&#34;和#34;明年&#34;,有几种方法可以使用PHP日期格式参数来解决这个问题,在PHP手册here中进行了解释。还需要检查上边界以确保日期不会超过下周/月/年。

  <?php
    $checkdate = strtotime('+8 days');
    $datenow = time();

    $diff = $checkdate - $datenow;

    echo "Today: ".date("Y-m-d", $datenow)."<br/>";
    echo "Target: ".date("Y-m-d", $checkdate)."<br/>";

    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    $years = floor($diff / (365*60*60*24));

    $daytoday = date ('N', $datenow);
    $dayofmonthtoday = date ('j', $datenow);
    $dayofmonthtarget = date ('j', $checkdate);
    $monthtoday = date ('n', $datenow);
    $monthtarget = date ('n', $checkdate);
    $yeartoday = date ('Y', $datenow);
    $yeartarget = date ('Y', $checkdate);
    // $dayofyeartoday = date('z', $datenow);
    // $dayofyeartarget = date('z', $checkdate);



    if ($days >= 8-$daytoday && $days <= 14-$daytoday) {
         echo "Next week (Monday onwards)<br/>";
    }

    if ($days >= 7-$daytoday && $days <= 13-$daytoday) {
         echo "Next week (Sunday onwards)<br/>";
    }

    if ($days >= 7 && $days <= 13) {
         echo "Next week (7-13 days)<br/>";
    }

    if ($monthtarget == $monthtoday + 1) {
         echo "Next month (Change in month)<br/>";
    }

    if ($monthtarget == $monthtoday + 1 && $dayofmonthtarget >= $dayofmonthtoday) {
         echo "Next month (Change in month but at least same day in the month and before end of that month)<br/>";
    }

    if ($yeartarget == $yeartoday + 1) {
         echo "Next year (Change in year)<br/>";
    }

    if ($yeartarget == $yeartoday + 1 && $years == 1) {
         echo "Next year (Change in year but at least same day and month in the year and before end of that year)<br/>";
    }

?>