Jawaban:
$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));
// Finally you will have the date you're looking for.
Saya membutuhkan fungsi serupa, kecuali untuk siklus bulanan (plus bulan, minus 1 hari). Setelah mencari SO beberapa lama, saya dapat membuat solusi plug-n-play ini:
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;
}
Contoh:
$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
Gunakan DateTime::add.
$start = new DateTime("2010-12-11", new DateTimeZone("UTC"));
$month_later = clone $start;
$month_later->add(new DateInterval("P1M"));
Saya menggunakan clone karena add memodifikasi objek asli, yang mungkin tidak diinginkan.
strtotime( "+1 month", strtotime( $time ) );
ini mengembalikan stempel waktu yang dapat digunakan dengan fungsi tanggal
$timememiliki nilai awal).
(date('d') > 28) ? date("mdY", strtotime("last day of next month")) : date("mdY", strtotime("+1 month"));
Ini akan mengkompensasi bulan Februari dan 31 hari bulan lainnya. Anda tentu saja dapat melakukan lebih banyak pemeriksaan untuk mendapatkan yang lebih tepat untuk format tanggal relatif 'hari ini bulan depan' (yang sayangnya tidak berfungsi, lihat di bawah), dan Anda juga dapat menggunakan DateTime.
Keduanya DateInterval('P1M')dan strtotime("+1 month")pada dasarnya secara membabi buta menambahkan 31 hari terlepas dari jumlah hari di bulan berikutnya.
Anda bisa menggunakan DateTime::modifyseperti ini:
$date = new DateTime('2010-12-11');
$date->modify('+1 month');
Lihat dokumentasi:
Saya menggunakan cara ini: -
$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;
//******************************************************************//
Harap pertama-tama Anda mengatur format tanggal Anda seperti 12-12-2012
Setelah digunakan, fungsi ini berfungsi dengan baik;
$date = date('d-m-Y',strtotime("12-12-2012 +2 Months");
Di sini 12-12-2012 adalah tanggal Anda dan +2 Bulan adalah kenaikan bulan;
Anda juga menambah Tahun, Tanggal
strtotime("12-12-2012 +1 Year");
Jawabannya adalah 12-12-2013
Terima kasih Jason, kiriman Anda sangat membantu. Saya memformat ulang dan menambahkan lebih banyak komentar untuk membantu saya memahami semuanya. Jika itu membantu siapa pun, saya telah mempostingnya di sini:
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
$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+1 month", $date));
Jika Anda ingin menambah hari, Anda juga bisa melakukannya
$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+5 day", $date));
Hanya memperbarui jawaban dengan metode sederhana untuk menemukan tanggal setelah tidak ada bulan. Karena jawaban terbaik yang diberi tanda tidak memberikan solusi yang benar.
<?php
$date = date('2020-05-31');
$current = date("m",strtotime($date));
$next = date("m",strtotime($date."+1 month"));
if($current==$next-1){
$needed = date('Y-m-d',strtotime($date." +1 month"));
}else{
$needed = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
}
echo "Date after 1 month from 2020-05-31 would be : $needed";
?>
letakkan tanggal di kotak input lalu klik tombol dapatkan hari dari tanggal di jquery
$(document).ready( function() {
$("button").click(function(){
var day = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var a = new Date();
$(".result").text(day[a.getDay()]);
});
});
<?php
$selectdata ="select fromd,tod from register where username='$username'";
$q=mysqli_query($conm,$selectdata);
$row=mysqli_fetch_array($q);
$startdate=$row['fromd'];
$stdate=date('Y', strtotime($startdate));
$endate=$row['tod'];
$enddate=date('Y', strtotime($endate));
$years = range ($stdate,$enddate);
echo '<select name="years" class="form-control">';
echo '<option>SELECT</option>';
foreach($years as $year)
{ echo '<option value="'.$year.'"> '.$year.' </option>'; }
echo '</select>'; ?>
Semua solusi yang disajikan tidak berfungsi dengan baik.
strtotime () dan DateTime :: add atau DateTime :: modifikasikan terkadang memberikan hasil yang tidak valid.
Contoh:
- 31.08.2019 + 1 bulan memberikan 01.10.2019 sebagai gantinya 30.09.2019
- 29.02.2020 + 1 tahun memberikan 01.03.2021 sebagai gantinya 28.02.2021
(diuji pada PHP 5.5, PHP 7.3)
Di bawah ini adalah fungsi saya berdasarkan ide yang diposting oleh Angelo yang memecahkan masalah:
// $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');
}
Contoh penggunaan:
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)