PHP处理时间的DateTime ,DateInterval 等类的使用

  • Post author:
  • Post category:php


<?php
/**
 * Created by PhpStorm.
 * User: Itboot
 * Date: 2019/1/16
 * Time: 16:50
 */

$datetime = new DateTime();

var_dump($datetime);

$dateset = new DateTime('2015-05-04 5:17 AM');

var_dump($dateset);

$dateseto = DateTime::createFromFormat('M j, Y H:i:s', 'Jan 2, 2019 14:14:55');

var_dump($dateseto);

//创建时间实例
$datetime = new DateTime('2014-01-01 14:00:00');
//创建长度为两周后
$interval = new DateInterval('P2W');
$datetime->add($interval);
//2014-01-01 14:00:00  两周后的时间
echo $datetime->format('Y-m-d H:i:s') . PHP_EOL;
结果:::
2014-01-15 14:00:00


//间隔2天   往后数10次
$timenow = new \Datetime();
$datetin = DateInterval::createFromDateString('-2 day');
$datePer = new \DatePeriod($timenow, $datetin, 10);

foreach ($datePer as $day) {
    echo $day->format('Y-m-d'), PHP_EOL;
}
结果:::
2019-01-17
2019-01-15
2019-01-13
2019-01-11
2019-01-09
2019-01-07
2019-01-05
2019-01-03
2019-01-01
2018-12-30
2018-12-28



版权声明:本文为qq_34690432原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。