php时间跨度,在PHP中执行与日期时间相关的操作

小编典典

PHP5

+的DateTime对象很有用,因为它知道leap时间和夏令时,但是它需要一些扩展才能真正解决问题。我写了下面的文章来解决类似的问题。find_WeekdaysFromThisTo()方法是蛮力的,但是,如果您的时间跨度小于2年,则可以相当快速地工作。

$tryme = new Extended_DateTime('2007-8-26');

$newer = new Extended_DateTime('2008-9-1');

print 'Weekdays From '.$tryme->format('Y-m-d').' To '.$newer->format('Y-m-d').': '.$tryme -> find_WeekdaysFromThisTo($newer) ."\n";

/* Output: Weekdays From 2007-08-26 To 2008-09-01: 265 */

print 'All Days From '.$tryme->format('Y-m-d').' To '.$newer->format('Y-m-d').': '.$tryme -> find_AllDaysFromThisTo($newer) ."\n";

/* Output: All Days From 2007-08-26 To 2008-09-01: 371 */

$timefrom = $tryme->find_TimeFromThisTo($newer);

print 'Between '.$tryme->format('Y-m-d').' and '.$newer->format('Y-m-d').' there are '.

$timefrom['years'].' years, '.$timefrom['months'].' months, and '.$timefrom['days'].

' days.'."\n";

/* Output: Between 2007-08-26 and 2008-09-01 there are 1 years, 0 months, and 5 days. */

class Extended_DateTime extends DateTime {

public function find_TimeFromThisTo($newer) {

$timefrom = array('years'=>0,'months'=>0,'days'=>0);

// Clone because we're using modify(), which will destroy the object that was passed in by reference

$testnewer = clone $newer;

$timefrom['years'] = $this->find_YearsFromThisTo($testnewer);

$mod = '-'.$timefrom['years'].' years';

$testnewer -> modify($mod);

$timefrom['months'] = $this->find_MonthsFromThisTo($testnewer);

$mod = '-'.$timefrom['months'].' months';

$testnewer -> modify($mod);

$timefrom['days'] = $this->find_AllDaysFromThisTo($testnewer);

return $timefrom;

} // end function find_TimeFromThisTo

public function find_YearsFromThisTo($newer) {

/*

If the passed is:

not an object, not of class DateTime or one of its children,

or not larger (after) $this

return false

*/

if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))

return FALSE;

$count = 0;

// Clone because we're using modify(), which will destroy the object that was passed in by reference

$testnewer = clone $newer;

$testnewer -> modify ('-1 year');

while ( $this->format('U') < $testnewer->format('U')) {

$count ++;

$testnewer -> modify ('-1 year');

}

return $count;

} // end function find_YearsFromThisTo

public function find_MonthsFromThisTo($newer) {

/*

If the passed is:

not an object, not of class DateTime or one of its children,

or not larger (after) $this

return false

*/

if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))

return FALSE;

$count = 0;

// Clone because we're using modify(), which will destroy the object that was passed in by reference

$testnewer = clone $newer;

$testnewer -> modify ('-1 month');

while ( $this->format('U') < $testnewer->format('U')) {

$count ++;

$testnewer -> modify ('-1 month');

}

return $count;

} // end function find_MonthsFromThisTo

public function find_AllDaysFromThisTo($newer) {

/*

If the passed is:

not an object, not of class DateTime or one of its children,

or not larger (after) $this

return false

*/

if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))

return FALSE;

$count = 0;

// Clone because we're using modify(), which will destroy the object that was passed in by reference

$testnewer = clone $newer;

$testnewer -> modify ('-1 day');

while ( $this->format('U') < $testnewer->format('U')) {

$count ++;

$testnewer -> modify ('-1 day');

}

return $count;

} // end function find_AllDaysFromThisTo

public function find_WeekdaysFromThisTo($newer) {

/*

If the passed is:

not an object, not of class DateTime or one of its children,

or not larger (after) $this

return false

*/

if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))

return FALSE;

$count = 0;

// Clone because we're using modify(), which will destroy the object that was passed in by reference

$testnewer = clone $newer;

$testnewer -> modify ('-1 day');

while ( $this->format('U') < $testnewer->format('U')) {

// If the calculated day is not Sunday or Saturday, count this day

if ($testnewer->format('w') != '0' && $testnewer->format('w') != '6')

$count ++;

$testnewer -> modify ('-1 day');

}

return $count;

} // end function find_WeekdaysFromThisTo

public function set_Day($newday) {

if (is_int($newday) && $newday > 0 && $newday < 32 && checkdate($this->format('m'),$newday,$this->format('Y')))

$this->setDate($this->format('Y'),$this->format('m'),$newday);

} // end function set_Day

public function set_Month($newmonth) {

if (is_int($newmonth) && $newmonth > 0 && $newmonth < 13)

$this->setDate($this->format('Y'),$newmonth,$this->format('d'));

} // end function set_Month

public function set_Year($newyear) {

if (is_int($newyear) && $newyear > 0)

$this->setDate($newyear,$this->format('m'),$this->format('d'));

} // end function set_Year

} // end class Extended_DateTime

2021-03-10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值