date_modify
(PHP 5 >= 5.1.0)
date_modify — Alters the timestamp
Описание
void date_modify
( DateTime $object
, string $modify
)
void DateTime::modify
( string $modify
)
Список параметров
- object
-
DateTime object.
- modify
-
String in a relative format accepted by strtotime().
Возвращаемые значения
Returns NULL on success or FALSE on failure.
Примеры
Пример #1 A date_modify() example
<?php
$date = new DateTime("2006-12-12");
$date->modify("+1 day");
echo $date->format("Y-m-d");
?>
Результат выполнения данного примера:
2006-12-13
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с датой и временем
- Дата и Время
- checkdate
- date_add
- date_create_from_format
- date_create_immutable_from_format
- date_create_immutable
- date_create
- date_date_set
- date_default_timezone_get
- date_default_timezone_set
- date_diff
- date_format
- date_get_last_errors
- date_interval_create_from_date_string
- date_interval_format
- date_isodate_set
- date_modify
- date_offset_get
- date_parse_from_format
- date_parse
- date_sub
- date_sun_info
- date_sunrise
- date_sunset
- date_time_set
- date_timestamp_get
- date_timestamp_set
- date_timezone_get
- date_timezone_set
- date
- getdate
- gettimeofday
- gmdate
- gmmktime
- gmstrftime
- idate
- localtime
- microtime
- mktime
- strftime
- strptime
- strtotime
- time
- timezone_abbreviations_list
- timezone_identifiers_list
- timezone_location_get
- timezone_name_from_abbr
- timezone_name_get
- timezone_offset_get
- timezone_open
- timezone_transitions_get
- timezone_version_get
Коментарии
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));
?>
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.
$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');
}
?>