php7设置时间,date_modify()

date_modify()

(PHP 5 >= 5.2.0, PHP 7)

别名DateTime::modify()

说明

此函数是该函数的别名:DateTime::modify()$cday - specified day of the week (0-6 where 0 is Sunday)

$currentDate - date of start

$endDate - date of end

We need dates of next couple of days, that day of week match defined.

if($currentDate->format('w')!= $cday){

switch ($cday){

case 0 : $cdays="Sunday"; break;

case 1 : $cdays="Monday"; break;

case 2 : $cdays="Tuesday"; break;

case 3 : $cdays="Wednesday"; break;

case 4 : $cdays="Thursday"; break;

case 5 : $cdays="Friday"; break;

case 6 : $cdays="Saturday";

}

date_modify($currentDate,"+1{$cdays}");

}

while($currentDate < $endDate) {

echo $currentDate -> format('Y-m-d H:i:s');

$currentDate -> modify('+1 week');

}

?>I have trouble finding the documentation for the dateTime object, but this seems to work:

$currentDate = new DateTime('2008-01-04');

$endDate = new DateTime('2009-01-04');

while($currentDate < $endDate) {

echo $currentDate -> format('Y-m-d') . ' till ';

$currentDate -> modify('+1 week');

echo $currentDate -> format('Y-m-d') . '
';

}

?>

This will (obviously) print a list of date-ranges between startdate and enddate.I decided to enhance the DateTime object by taking advantage of method chaining.

class DateTimeChain extends DateTime {

public function modify ($modify) {

parent::modify($modify);

return $this;

}

public function setDate ($year, $month, $day) {

parent::setDate($year, $month, $day);

return $this;

}

public function setISODate ($year, $week, $day = null) {

parent:: setISODate($year, $week, $day);

return $this;

}

public function setTime ($hour, $minute, $second = null) {

parent::setTime($hour, $minute, $second);

return $this;

}

public function setTimezone ($timezone) {

parent::setTimezone($timezone);

return $this;

}

}

$t = new DateTimeZone('America/Los_Angeles');

$d = new DateTimeChain();

var_dump($d->setTimezone($t)->modify('5years')->format(DATE_RFC822));

?>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值