PHP 5.4.36 Released

DateTime::setDate

(PHP 5 >= 5.2.0)

DateTime::setDateتنظیم تاریخ

Description

Oriented object style

public DateTime DateTime::setDate ( int $year , int $month , int $day )

Procedural style

DateTime date_date_set ( DateTime $object , int $year , int $month , int $day )

بازنشانی تاریخ فعلی شی DateTime به تاریخ دیگر.

Parameters

object

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

year

سال تاریخ.

month

ماه تاریخ.

day

روز تاریخ.

Return Values

Returns the DateTime object for method chaining or FALSE on failure.

Changelog

Version Description
5.3.0Changed the return value from NULL to DateTime.

Examples

Example #1 مثال DateTime::setDate()

Oriented object style

<?php
$date 
= new DateTime();
$date->setDate(200123);
echo 
$date->format('Y-m-d');
?>

Procedural style

<?php
$date 
date_create();
date_date_set($date200123);
echo 
date_format($date'Y-m-d');
?>

The above examples will output:

2001-02-03

Example #2 مقادیر خارج از بازه به مقادیر والد آنها اضافه می‌شوند

<?php
$date 
= new DateTime();

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

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

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

The above example will output:

2001-02-28
2001-03-01
2002-02-03

See Also

add a note add a note

User Contributed Notes 1 note

up
5
remy215 at laposte dot net
2 years ago
Be warned, DateTime::setDate() does not check for invalid input.

Illustration:
<?php
$dt
= new DateTime();
$dt->setDate(2012, 11, 31); // returns DateTime object and not false although this date does not exist
echo $dt->format('Y-m-d'); // output: 2012-12-01
?>

No error was generated on entering a non existing date, php silently changed it.
To Top