Với PHP, tôi muốn tăng ngày thêm một tháng và tôi muốn năm tự động tăng lên, nếu cần [tôi. e. tăng từ tháng 12 năm 2012 đến tháng 1 năm 2013]
Trân trọng
đã hỏi 20 tháng 5 năm 2010 lúc 0. 15
1
$time = strtotime["2010.12.11"];
$final = date["Y-m-d", strtotime["+1 month", $time]];
// Finally you will have the date you're looking for.
Joeri
2.00921 huy hiệu bạc22 huy hiệu đồng
đã trả lời 20 tháng 5 năm 2010 lúc 0. 45
Raphael Caixeta Raphael Caixeta
7.7649 huy hiệu vàng50 huy hiệu bạc76 huy hiệu đồng
6
Tôi cần chức năng tương tự, ngoại trừ chu kỳ hàng tháng [cộng tháng, trừ 1 ngày]. Sau khi tìm kiếm S. O. trong một thời gian, tôi đã có thể tạo ra giải pháp plug-n-play này
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;
}
Ví dụ
$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
Alfie
2.2862 huy hiệu vàng26 huy hiệu bạc43 huy hiệu đồng
đã trả lời 3 tháng 6 năm 2014 lúc 11. 55
Jason Jason
8349 huy hiệu bạc13 huy hiệu đồng
7
Sử dụng
$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
9$start = new DateTime["2010-12-11", new DateTimeZone["UTC"]];
$month_later = clone $start;
$month_later->add[new DateInterval["P1M"]];
Tôi đã sử dụng bản sao vì add sửa đổi đối tượng ban đầu, điều này có thể không được mong muốn
đã trả lời 20 tháng 5 năm 2010 lúc 0. 17
Matthew Flaschen Matthew Flaschen
271k49 huy hiệu vàng510 huy hiệu bạc537 huy hiệu đồng
1
strtotime[ "+1 month", strtotime[ $time ] ];
cái này trả về dấu thời gian có thể được sử dụng với hàm ngày
đã trả lời 20 tháng 5 năm 2010 lúc 0. 18
Galen Galen
29. 8k9 huy hiệu vàng70 huy hiệu bạc89 huy hiệu đồng
4
Bạn có thể sử dụng
$start = new DateTime["2010-12-11", new DateTimeZone["UTC"]];
$month_later = clone $start;
$month_later->add[new DateInterval["P1M"]];
0 như thế này$date = new DateTime['2010-12-11'];
$date->modify['+1 month'];
xem tài liệu
https. //php. net/manual/en/datetime. biến đổi. php
https. //php. net/manual/en/class. ngày giờ. php
CẬP NHẬT tháng 1 năm 2021. sửa lỗi do nhận xét nêu ra
Giải pháp này có một số vấn đề đối với các tháng có 31 ngày như tháng 5, v.v.
Ví dụ. điều này nhảy từ ngày 31 tháng 5 đến ngày 1 tháng 7 không chính xác
Để khắc phục điều đó, bạn có thể tạo chức năng tùy chỉnh này
________số 8Sau đó, bạn có thể sử dụng nó như thế
$date = new DateTime['2010-05-31'];
addMonths[$date, 1];
print_r[$date];
//DateTime Object [ [date] => 2010-06-30 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ]
Giải pháp này đã được tìm thấy trong PHP. net đăng bởi jenspj. https. //www. php. net/manual/fr/datetime. biến đổi. php#107592
tòa nhà
18. 3k10 huy hiệu vàng33 huy hiệu bạc49 huy hiệu đồng
đã trả lời 1 tháng 11 năm 2017 lúc 11. 49
HRoux HRoux
3649 huy hiệu bạc12 huy hiệu đồng
3
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;
}
0Điều này sẽ bù cho tháng 2 và các tháng có 31 ngày khác. Tất nhiên, bạn có thể kiểm tra nhiều hơn để có được chính xác hơn cho các định dạng ngày tương đối của 'ngày này tháng sau' [đáng buồn là không hoạt động, xem bên dưới] và bạn cũng có thể sử dụng DateTime
Cả
$start = new DateTime["2010-12-11", new DateTimeZone["UTC"]];
$month_later = clone $start;
$month_later->add[new DateInterval["P1M"]];
1 và $start = new DateTime["2010-12-11", new DateTimeZone["UTC"]];
$month_later = clone $start;
$month_later->add[new DateInterval["P1M"]];
2 về cơ bản đều cộng 31 ngày một cách mù quáng bất kể số ngày trong tháng tiếp theo là bao nhiêu- 31-01-2010 => Ngày 3 tháng 3
- 31-01-2012 => Ngày 2 tháng 3 [năm nhuận]
đã trả lời 10/11/2014 lúc 22. 07
Wayne Weibel Wayne Weibel
9031 huy hiệu vàng14 huy hiệu bạc22 huy hiệu đồng
1
Trước tiên, bạn hãy đặt định dạng ngày của mình như ngày 12-12-2012
Sau khi sử dụng chức năng này, nó hoạt động bình thường;
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;
}
3Ở đây 12-12-2012 là ngày của bạn và +2 Tháng là gia số của tháng;
Bạn cũng tăng Năm, Ngày
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;
}
0Đáp án là ngày 12-12-2013
Pang
9.173146 huy hiệu vàng84 huy hiệu bạc118 huy hiệu đồng
đã trả lời 15/11/2016 lúc 9. 32
Pravin Suthar Pravin Suthar
1.3551 huy hiệu vàng13 huy hiệu bạc25 huy hiệu đồng
tôi sử dụng cách này. -
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;
}
1đã trả lời 19 tháng 1 năm 2015 lúc 5. 42
cây nho cây nho
13. 2k10 huy hiệu vàng53 huy hiệu bạc75 huy hiệu đồng
3
Chỉ cần cập nhật câu trả lời bằng phương pháp đơn giản để tìm ngày sau số tháng. Vì câu trả lời hay nhất được đánh dấu không đưa ra giải pháp chính xác
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;
}
2đã trả lời 22 tháng 11 năm 2019 lúc 12. 52
Ashok Ashok
7149 huy hiệu bạc20 huy hiệu đồng
2
Nếu bạn muốn lấy ngày của một tháng kể từ bây giờ, bạn có thể làm như thế này
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;
}
3Nếu bạn muốn lấy ngày của hai tháng kể từ bây giờ, bạn có thể đạt được điều đó bằng cách làm điều này
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;
}
4Và như vậy, đó là tất cả
Pháp sư ♦
27. 7k21 huy hiệu vàng75 huy hiệu bạc126 huy hiệu đồng
đã trả lời 6 tháng 9 năm 2020 lúc 4. 34
1
Cảm ơn Jason, bài viết của bạn rất hữu ích. Mình đã format lại và bổ sung thêm comment để ae hiểu hết. Trong trường hợp giúp được bất cứ ai, tôi đã đăng nó ở đây
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;
}
5đã trả lời 13/11/2016 ở 20. 14
Greg Greg
6111 huy hiệu vàng8 huy hiệu bạc15 huy hiệu đồng
1
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;
}
6Nếu bạn muốn tăng theo ngày, bạn cũng có thể làm điều đó
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;
}
7đã trả lời ngày 6 tháng 5 năm 2017 lúc 8. 13
1
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;
}
8ví dụ sử dụng
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;
}
9đã trả lời 22 tháng 12 năm 2016 lúc 10. 28
T30 T30
10. 6k6 huy hiệu vàng51 huy hiệu bạc57 huy hiệu đồng
Đối với bất kỳ ai đang tìm kiếm câu trả lời cho bất kỳ định dạng ngày nào
$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
0Chỉ cần thay đổi định dạng ngày
đã trả lời Ngày 1 tháng 6 năm 2017 lúc 17. 17
Vải Vải
2421 huy hiệu bạc8 huy hiệu đồng
$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
1đã trả lời 30 tháng 9 năm 2020 lúc 4. 56
1
Tôi thấy hàm mtkime[] hoạt động rất tốt cho việc này
$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
2kết quả. 2021-11-01
Tôi muốn lấy 'ngày' trừ 1 để ra '31-10-2021'. Điều này có thể hữu ích nếu bạn muốn hiển thị một phạm vi trong 12 tháng, e. g. Ngày 1 tháng 10 năm 2021 đến ngày 30 tháng 9 năm 2022