略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: date_modify

2024-12-22

date_modify

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

date_modify别名 DateTime::modify()

说明

此函数是该函数的别名: DateTime::modify()

add a noteadd a note

User Contributed Notes 3 notes

up
-2
ochojnackiATEMEgmail.com
13 years ago
$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.

<?php

           
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');
            }

?>
up
-4
matthijs at yourmediafactory dot com
14 years ago
I have trouble finding the documentation for the dateTime object, but this seems to work:

<?php
$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') . ' <br />';
}
?>

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

<?php

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));

?>

官方地址:https://www.php.net/manual/en/function.date-modify.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3