Diese Funktion ist ein Alias für: DateTime::modify()
Fungsi untuk menambah/mengurangi Tanggal, misalnya 1984-08-01 ditambah 1 hari menjadi 1984-08-02
Function for add/decrease date, example 1984-08-01 add 1 day = 1984-08-02
-----------------------------------------------------------
<?php
function adddate($vardate,$added)
{
$data = explode("-", $vardate);
$date = new DateTime();
$date->setDate($data[0], $data[1], $data[2]);
$date->modify("".$added."");
$day= $date->format("Y-m-d");
return $day;
}
echo "Example : " . adddate("2010-08-01","+1 day");
//--hasil---- Example : 2010-08-02
?>
-----------------------------------------------
Lihat juga utk variabel added / See also for variable added :
+1 day , -1 day , +3 month , -2 month and etc
I found a rather unusual problem when using the modify function to add months to a date object. Unsure if it's bug or not, but I thought I'd post it here to warn others.
When you use the modify function to add months like this:
<?php
$date = new DateTime();
$date->setDate(2009, 1, 31);
$date->modify("+1 month");
print $date->format("Y-n-j");
?>
You will notice that the output is not 2009-2-31 but 2009-3-3. The reason for this is obvious when you look at it. Since February does not have 31 days, the object itself just counts on and sets the month to March the 3rd. However this is not what you would expect when you use it to add months in a loop, because it will skip February in this case.
A fix for this is in the next block of code. It changes the day of the month to the maximum number of days of the next month when it does not fit. Can't really say it's the best solution for the problem but it does what you'd expect:
<?php
class DateTimeExt extends DateTime
{
public function modify($modify)
{
$match = "";
if (preg_match("/(-|\+)(\d+) months{0,1}/", $modify, $match))
{
$month = ($match[1] == "-") ? $this->format("n") - $match[2] : $this->format("n") + $match[2];
$tmp = clone($this);
$tmp->setDate($this->format("Y"), $month, 1);
$this->setDate($this->format("Y"), $month, $this->format("j"));
if ($tmp->format("n") != $this->format("n"))
{
$this->setDate($this->format("Y"), $tmp->format("n"), $tmp->format("t"));
}
}
// everything else
parent::modify($modify);
}
}
?>
$cday - specified day of the week (0-6 where 0 is Sunday)
$currentDate - date of start
$endDate - date of end
We need dates of next couple of days, that day of week match defined.
<?php
if($currentDate->format('w')!= $cday){
switch ($cday){
case 0 : $cdays="Sunday"; break;
case 1 : $cdays="Monday"; break;
case 2 : $cdays="Tuesday"; break;
case 3 : $cdays="Wednesday"; break;
case 4 : $cdays="Thursday"; break;
case 5 : $cdays="Friday"; break;
case 6 : $cdays="Saturday";
}
date_modify($currentDate,"+1 {$cdays}");
}
while($currentDate < $endDate) {
echo $currentDate -> format('Y-m-d H:i:s');
$currentDate -> modify('+1 week');
}
?>
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.
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));
?>
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'));
}
?>