PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

date_offset_get> <date_isodate_set
Last updated: Fri, 20 Jun 2008

view this page in

date_modify

(PHP 5 >= 5.1.0)

date_modify — Modifie le timestamp

Description

void date_modify ( DateTime $object , string $modify )
void DateTime::modify ( string $modify )

Liste de paramètres

object

Objet DateTime.

modify

Une chaîne de caractères dans le format relatif accepté par strtotime().

Valeurs de retour

Retourne NULL en cas de succès ou FALSE en cas d'échec.

Exemples

Exemple #1 Exemple avec date_modify()

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

L'exemple ci-dessus va afficher :

2006-12-13

Voir aussi



date_offset_get> <date_isodate_set
Last updated: Fri, 20 Jun 2008
 
add a note add a note User Contributed Notes
date_modify
matthijs at yourmediafactory dot com
08-Dec-2007 04:38
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.
someone
14-Sep-2007 11:46
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));

?>
mike_d_olson [at] yahoo [dot] no-spam
08-Aug-2007 04:17
I had problems with setting an existing DateTime object to an exact Unix timestamp using modify("@$timestamp"), which seems to always be relative.  So I wrote this function, which does the trick:

<?php
   
function set_time(DateTime $dt, $timestamp)
    {
       
$tzo = new DateTimeZone($dt->getTimezone()->getName());
       
$new_dt = new DateTime("@$timestamp", new DateTimeZone('UTC'));
       
$new_dt->setTimezone($tzo);
       
$dt->setDate($new_dt->format('Y'), $new_dt->format('m'), $new_dt->format('d'));
       
$dt->setTime($new_dt->format('H'), $new_dt->format('i'), $new_dt->format('s'));
    }
?>

date_offset_get> <date_isodate_set
Last updated: Fri, 20 Jun 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites