update : 2015.11.03
php.shukuma.com

검색:
 
 
Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object

DateTime::sub

date_sub

(PHP 5 >= 5.3.0, PHP 7)

DateTime::sub -- date_sub Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object

설명

객체 기반 형식

public DateTime DateTime::sub ( DateInterval $interval )

절차식 형식

DateTime date_sub ( DateTime $object , DateInterval $interval )

Subtracts the specified DateInterval object from the specified DateTime object.

인수

object

Procedural style only: A DateTime object returned by date_create(). The function modifies this object.

interval

A DateInterval object

반환값

메소드 체이닝을 위한 DateTime 객체를 반환합니다.실패 시 FALSE를 반환합니다.

예제

Example #1 DateTime::sub() example

객체 기반 형식

<?php
$date 
= new DateTime('2000-01-20');
$date->sub(new DateInterval('P10D'));
echo 
$date->format('Y-m-d') . "\n";
?>

절차식 형식

<?php
$date 
date_create('2000-01-20');
date_sub($datedate_interval_create_from_date_string('10 days'));
echo 
date_format($date'Y-m-d');
?>

위 예제들의 출력:

2000-01-10

Example #2 Further DateTime::sub() examples

<?php
$date 
= new DateTime('2000-01-20');
$date->sub(new DateInterval('PT10H30S'));
echo 
$date->format('Y-m-d H:i:s') . "\n";

$date = new DateTime('2000-01-20');
$date->sub(new DateInterval('P7Y5M4DT4H3M2S'));
echo 
$date->format('Y-m-d H:i:s') . "\n";
?>

위 예제의 출력:

2000-01-19 13:59:30
1992-08-15 19:56:58

Example #3 Beware when subtracting months

<?php
$date 
= new DateTime('2001-04-30');
$interval = new DateInterval('P1M');

$date->sub($interval);
echo 
$date->format('Y-m-d') . "\n";

$date->sub($interval);
echo 
$date->format('Y-m-d') . "\n";
?>

위 예제의 출력:

2001-03-30
2001-03-02

주의

DateTime::modify() is an alternative when using PHP 5.2.

참고