Nehmen wir an, ich habe ein Datum in folgendem Format: 2010-12-11 (year-mon-day)
Mit PHP möchte ich das Datum um einen Monat erhöhen, und ich möchte, dass das Jahr bei Bedarf automatisch inkrementiert wird (d. H. Von Dezember 2012 auf Januar 2013 erhöht).
Grüße.
$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));
// Finally you will have the date you're looking for.
Ich brauchte eine ähnliche Funktion mit Ausnahme eines monatlichen Zyklus (plus Monate, minus 1 Tag). Nach dem Suchen von S.O. Für eine Weile konnte ich diese Plug-n-Play-Lösung herstellen:
function add_months($months, DateTime $dateObject)
{
$next = new DateTime($dateObject->format('Y-m-d'));
$next->modify('last day of +'.$months.' month');
if($dateObject->format('d') > $next->format('d')) {
return $dateObject->diff($next);
} else {
return new DateInterval('P'.$months.'M');
}
}
function endCycle($d1, $months)
{
$date = new DateTime($d1);
// call second function to add the months
$newDate = $date->add(add_months($months, $date));
// goes back 1 day from date, remove if you want same day of month
$newDate->sub(new DateInterval('P1D'));
//formats final date to Y-m-d form
$dateReturned = $newDate->format('Y-m-d');
return $dateReturned;
}
Beispiel:
$startDate = '2014-06-03'; // select date in Y-m-d format
$nMonths = 1; // choose how many months you want to move ahead
$final = endCycle($startDate, $nMonths); // output: 2014-07-02
Verwenden Sie DateTime::add
.
$start = new DateTime("2010-12-11", new DateTimeZone("UTC"));
$month_later = clone $start;
$month_later->add(new DateInterval("P1M"));
Ich habe clone verwendet, weil add das Originalobjekt verändert, was möglicherweise nicht erwünscht ist.
strtotime( "+1 month", strtotime( $time ) );
dies gibt einen Zeitstempel zurück, der mit der Datumsfunktion verwendet werden kann
Ich benutze diesen Weg: -
$occDate='2014-01-28';
$forOdNextMonth= date('m', strtotime("+1 month", strtotime($occDate)));
//Output:- $forOdNextMonth=02
/*****************more example****************/
$occDate='2014-12-28';
$forOdNextMonth= date('m', strtotime("+1 month", strtotime($occDate)));
//Output:- $forOdNextMonth=01
//***********************wrong way**********************************//
$forOdNextMonth= date('m', strtotime("+1 month", $occDate));
//Output:- $forOdNextMonth=02; //instead of $forOdNextMonth=01;
//******************************************************************//
(date('d') > 28) ? date("mdY", strtotime("last day of next month")) : date("mdY", strtotime("+1 month"));
Dies wird den Februar und die anderen 31 Tage kompensieren. Sie können natürlich noch viel mehr Nachprüfungen anstellen, um für "diesen Tag nächsten Monat" relative Datumsformate genauer zu werden (was leider nicht funktioniert, siehe unten), und Sie können auch DateTime verwenden.
Sowohl DateInterval('P1M')
als auch strtotime("+1 month")
addieren im Wesentlichen blind 31 Tage, unabhängig von der Anzahl der Tage im Folgemonat.
Sie können DateTime::modify
folgendermaßen verwenden:
$date = new DateTime('2010-12-11');
$date->modify('+1 month');
Siehe Dokumentationen:
Bitte stellen Sie zuerst Ihr Datumsformat auf den 12-12-2012 ein
Nach Gebrauch dieser Funktion funktioniert es einwandfrei.
$date = date('d-m-Y',strtotime("12-12-2012 +2 Months");
Hier ist der 12-12-2012 Ihr Datum und +2 Monate ist das Inkrement des Monats;
Sie erhöhen außerdem Jahr, Datum
strtotime("12-12-2012 +1 Year");
Ans ist 12-12-2013
Danke Jason, dein Beitrag war sehr hilfreich. Ich habe es neu formatiert und weitere Kommentare hinzugefügt, um mir zu helfen, alles zu verstehen. Falls das jemandem hilft, habe ich es hier gepostet:
function cycle_end_date($cycle_start_date, $months) {
$cycle_start_date_object = new DateTime($cycle_start_date);
//Find the date interval that we will need to add to the start date
$date_interval = find_date_interval($months, $cycle_start_date_object);
//Add this date interval to the current date (the DateTime class handles remaining complexity like year-ends)
$cycle_end_date_object = $cycle_start_date_object->add($date_interval);
//Subtract (sub) 1 day from date
$cycle_end_date_object->sub(new DateInterval('P1D'));
//Format final date to Y-m-d
$cycle_end_date = $cycle_end_date_object->format('Y-m-d');
return $cycle_end_date;
}
//Find the date interval we need to add to start date to get end date
function find_date_interval($n_months, DateTime $cycle_start_date_object) {
//Create new datetime object identical to inputted one
$date_of_last_day_next_month = new DateTime($cycle_start_date_object->format('Y-m-d'));
//And modify it so it is the date of the last day of the next month
$date_of_last_day_next_month->modify('last day of +'.$n_months.' month');
//If the day of inputted date (e.g. 31) is greater than last day of next month (e.g. 28)
if($cycle_start_date_object->format('d') > $date_of_last_day_next_month->format('d')) {
//Return a DateInterval object equal to the number of days difference
return $cycle_start_date_object->diff($date_of_last_day_next_month);
//Otherwise the date is easy and we can just add a month to it
} else {
//Return a DateInterval object equal to a period (P) of 1 month (M)
return new DateInterval('P'.$n_months.'M');
}
}
$cycle_start_date = '2014-01-31'; // select date in Y-m-d format
$n_months = 1; // choose how many months you want to move ahead
$cycle_end_date = cycle_end_date($cycle_start_date, $n_months); // output: 2014-07-02
Alle vorgestellten Lösungen funktionieren nicht richtig.
strtotime () und DateTime :: add oder DateTime :: modify liefern manchmal ungültige Ergebnisse.
Beispiele:
- 31.08.2019 + 1 Monat ergibt 01.10.2019 statt 30.09.2019
- 29.02.2020 + 1 Jahr ergibt 01.03.2021 statt 28.02.2021
(getestet auf PHP 5.5, PHP 7.3)
Unten ist meine Funktion basierend auf Idee gepostet von Angelo , die das Problem löst:
// $time - unix time or date in any format accepted by strtotime() e.g. 2020-02-29
// $days, $months, $years - values to add
// returns new date in format 2021-02-28
function addTime($time, $days, $months, $years)
{
// Convert unix time to date format
if (is_numeric($time))
$time = date('Y-m-d', $time);
try
{
$date_time = new DateTime($time);
}
catch (Exception $e)
{
echo $e->getMessage();
exit;
}
if ($days)
$date_time->add(new DateInterval('P'.$days.'D'));
// Preserve day number
if ($months or $years)
$old_day = $date_time->format('d');
if ($months)
$date_time->add(new DateInterval('P'.$months.'M'));
if ($years)
$date_time->add(new DateInterval('P'.$years.'Y'));
// Patch for adding months or years
if ($months or $years)
{
$new_day = $date_time->format("d");
// The day is changed - set the last day of the previous month
if ($old_day != $new_day)
$date_time->sub(new DateInterval('P'.$new_day.'D'));
}
// You can chage returned format here
return $date_time->format('Y-m-d');
}
Anwendungsbeispiele:
echo addTime('2020-02-29', 0, 0, 1); // add 1 year (result: 2021-02-28)
echo addTime('2019-08-31', 0, 1, 0); // add 1 month (result: 2019-09-30)
echo addTime('2019-03-15', 12, 2, 1); // add 12 days, 2 months, 1 year (result: 2019-09-30)
function dayOfWeek($date){
return DateTime::createFromFormat('Y-m-d', $date)->format('N');
}
Anwendungsbeispiele:
echo dayOfWeek(2016-12-22);
// "4"
echo dayOfWeek(date('Y-m-d'));
// "4"
Für alle, die eine Antwort auf jedes Datumsformat suchen.
echo date_create_from_format('d/m/Y', '15/04/2017')->add(new DateInterval('P1M'))->format('d/m/Y');
Ändern Sie einfach das Datumsformat.
$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+1 month", $date));
Wenn Sie die Anzahl der Tage erhöhen möchten, können Sie dies auch tun
$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+5 day", $date));