date_modify

(PHP 5 >= 5.1.0RC1)

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

参见

strtotime()


add a note add a note User Contributed Notes
mike_d_olson [at] yahoo [dot] no-spam
08-Aug-2007 11: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'));
   }
?>