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


If date is next week/next month/ next year

我想知道是否有可能使用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
}

我知道我的代码不会工作,但我正在寻找的东西,将实现类似的东西吗?

Easy Going with 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";
}
?>

请记住,使用>=需要您首先检查年,然后是月,然后是周,因为strtotime("next year") > strtotime("next month") > strtotime("next week")

这可以通过DateTime对象实现,使用diff来查看两者之间的时间,像这样的东西应该可以做到:

// 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
}

根据"下周"、"下个月"answers"明年"的各种定义,有几种方法可以使用PHP日期格式参数来实现这一点,在这里的PHP手册中有解释。还需要检查上边界,以确保日期不会超过下周/月/年。

  <?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/>";
    }
?>