How to get every 15th and last day of the month in PHP

前端 未结 5 1303
执笔经年
执笔经年 2021-01-13 03:21

I am trying to figure out on how to set my dates to every 15th and end of month only...what i\'m getting so far is only +15 days from my current date.. current date = date t

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 03:45

    Try this

    1. Find the first day of month
    2. Find the Last Day of month as Lalji Nakum told
    3. Check your date with 15th Day of month Like example bellow
    if($today < $hDay){
        echo 'Start : '. date("t-m-Y");
    }else if ($today >= $hDay){
        echo 'Start :'. $hDay;
    }
    

    Here, 1. $today will the current date, 2. $hDay will be the 15th day of the month like 15 Nov

    Here is the sample code

    echo 'First day of month '.
    $fDay = date('01-m-Y');
    echo '
    15th day of month '. $hDay = date('d-m-Y', (strtotime($fDay)+ (86400 * 15))); echo '
    Last Day of month '. $lDay = date("t-m-Y"); echo '
    Current day '. $today = date('d-m-Y'); //$today = date('d-m-Y', strtotime($hDay)+86400 ); echo '
    '; if($today < $hDay){ echo 'Start : '. $lDay = date("t-m-Y"); }else if ($today >= $hDay){ echo 'Start :'. $hDay; }

提交回复
热议问题