PHP DatePeriod 周期

/**
 * DatePeriod
 *
 * 3rd component: https://github.com/briannesbitt/Carbon
 */


/**
 * DateInterval 表示到下一个周期的时间间隔.
 *
 * 2W表示两周.
 */
$interval = new DateInterval('P2W');
$start = new DateTime();

$periods = new DatePeriod($start, $interval, 3, DatePeriod::EXCLUDE_START_DATE);

foreach($periods as $nextDateTime) {
  var_dump($nextDateTime->format('Y-m-d'));
}

面向对象进阶之:closure

/**
 * Class App
 *
 * 使用Closure bindTo实现路由添加.
 */
class App {
  protected $routers = [];
  protected $responseStatus = 200;
  protected $responseType = 'text/html';
  protected $responseBody = 'hello world';

  public function addRoute($routePath, Closure $routeCallback) {
    $this->routers[$routePath] = $routeCallback->bindTo($this, __CLASS__);
    return $this;
  }

  public function dispatch($currentPath = '') {
    if ($currentPath && isset($this->routers[$currentPath])) {
      $this->routers[$currentPath]();
    }

    header('HTTP/1.1 ' . $this->responseStatus);
    header('Content-type: ' . $this->responseType);
    header('Content-length: ' . mb_strlen($this->responseBody));
    print $this->responseBody;
  }

  public static function factory() {
    return new static();
  }
}

App::factory()->addRoute('home', function() {
  $this->responseBody = 'this is front page';
})->addRoute('news', function() {
  $this->responseBody = 'this is news page';
})->dispatch('news');