开发者

php - check difference on dates + additional time

What would be a good way to check if point is between start and extra.

point = 2010-06-20 
start = 2010-06-17
extra = start + "1 week"

Any ideas would be appreciate开发者_JAVA技巧 it.


take a look at strtotime - an then simply compare the resulting timestamps:

$start = strtotime('2010-06-20');
$point = strtotime('2010-06-17');
$extra = strtotime('+1 week', $start);

if($start < $point && $extra > $point){
  // it's bewtween...
}


Requires PHP 5.3

$period = new DatePeriod(
    new DateTime('2010-06-17'),
    DateInterval::createFromDateString('+1 day'),
    new DateTime('2010-06-17 +1 week')
);

if (in_array(new DateTime('2010-06-20'), iterator_to_array($period))) {
    // date is in range
}

Manual http://de2.php.net/manual/en/dateperiod.construct.php


I'd probably extend the DatePeriod class to have a contains methods:

class DateRange extends DatePeriod
{
    public function contains(DateTime $dateTime)
    {
        return in_array($dateTime, iterator_to_array($this));
    }
}

then you can do

$period = new DateRange(
    new DateTime('2010-06-17'),
    DateInterval::createFromDateString('+1 day'),
    new DateTime('2010-06-17 +1 week')
);
if ($period->contains(new DateTime('2011-06-20'))) {
    // date is in range
}


try this

$start_timestamp = strtotime('2010-05-17');
$end_timestamp   = strtotime(date("Y-m-d", $start_timestamp) . " +1 week");
$point_timestamp = strtotime('2010-16-20');

if ($point_timestamp < $end_timestamp && $point_timestamp > $point_timestamp) {
// Do your work
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜