PHP Doku:: Alters the timestamp - datetime.modify.html

Verlauf / Chronik / History: (6) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatums- und zeitrelevante ErweiterungenDatum und UhrzeitThe DateTime classDateTime::modify

Ein Service von Reinhard Neidl - Webprogrammierung.

The DateTime class

<<DateTime::getTimezone

DateTime::__set_state>>

DateTime::modify

(PHP 5 >= 5.2.0)

DateTime::modifyAlters the timestamp

Beschreibung

Objektorientierter Stil

public DateTime DateTime::modify ( string $modify )

Prozeduraler Stil

DateTime date_modify ( DateTime $object , string $modify )

Alter the timestamp of a DateTime object by incrementing or decrementing in a format accepted by strtotime().

Parameter-Liste

object

Nur bei prozeduralem Aufruf: Ein von date_create() zurückgegebenes DateTime-Objekt. Diese Funktion verändert dieses Objekt.

modify

A date/time string. Gültige Formate werden unter Datums- und Zeitformate erläutert.

Rückgabewerte

Returns the DateTime object for method chainingIm Fehlerfall wird FALSE zurückgegeben..

Changelog

Version Beschreibung
5.3.0Der Rückgabewert wurde von NULL auf DateTime geändert.

Beispiele

Beispiel #1 DateTime::modify() example

Objektorientierter Stil

<?php
$date 
= new DateTime('2006-12-12');
$date->modify('+1 day');
echo 
$date->format('Y-m-d');
?>

Prozeduraler Stil

<?php
$date 
date_create('2006-12-12');
date_modify($date'+1 day');
echo 
date_format($date'Y-m-d');
?>

The above examples will output:

2006-12-13

Beispiel #2 Beware when adding or subtracting months

<?php
$date 
= new DateTime('2000-12-31');

$date->modify('+1 month');
echo 
$date->format('Y-m-d') . "\n";

$date->modify('+1 month');
echo 
$date->format('Y-m-d') . "\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

2001-01-31
2001-03-03

Siehe auch


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
tom at r dot je
18.06.2009 16:43
If you want to find the next working day (assuming mon-fri) you can use this:

<?php
$d
= new DateTime();

$day = $d->format('w');

if (
$day == 0 || $day >= 5) $d->modify('+' . ((7-$day+1) % 7) . ' days');
else
$d->modify('+1 day');
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)