簡體   English   中英

給定“開始日期”中的PHP“結束日期”-下個月的最后一天/ 15日

[英]PHP 'end date' from a given 'start date' - last day/15th of the following month

我想從PHP中的給定“開始日期”中獲取“結束日期”。 結束日期基於開始日期,其計算方法如下:

如果開始日期是該月的1日至15日,則結束日期是下個月的15日。

如果開始日期是該月的16-31,則結束日期是下個月的最后一天。

例如:$ start_date ='2009-11-23';

能行嗎?

$start_timestamp = strtotime('2009-11-17');
$d1 = getdate($start_timestamp);

$end_timestamp = mktime(
    0,
    0,
    0,
    $d1['mon'] + 1 + floor($d1['mday']/16),   // 1 before the 16th, then 2
    15 * (1-floor($d1['mday']/16)),        //15 before the 16th, then 0
    $d1['year']
);
$end_date = date('Y-m-d', $end_timestamp);

這是另一種方法:

$dt = new DateTime($start_date);
if ($dt->format('d') > 15) {
    $day = 'last day';
} else {
    $day = (15 - $dt->format('d')) . ' days';
}
echo $dt->modify('next month ' . $day)->format('Y-m-d');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM