如何在PHP中获得每月的第15天和最后一天 [英] How to get every 15th and last day of the month in PHP

查看:75
本文介绍了如何在PHP中获得每月的第15天和最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何将日期设置为每月的第15天和月底...到目前为止,我得到的只是距当前日期+15天。.
当前日期=今天的日期逻辑:如果当前日期小于15,则循环的开始日期在月末,则如果当前日期等于或大于15,则循环的开始日期在15日。因此,在我的情况下,我的当前日期是11月9日,因此第一个输出应为11月30日。

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 today.LOGIC: if the current day is less than 15 then the start date of the loop is on the end of the month then if the current day is equal or greater than 15 then the start date of the loop is on the 15th. so in my case my current date is Nov 9 so the First output should be Nov 30.

$y = 1;
while ($y <= $num_term) { // num_term is equal to the number of output
    $month_line = strtotime("15 day", strtotime("$month_sched"));
    $day = date("d", $month_line);
    $month_int = date("M-d", $month_line);
} 

输出:

Nov 24`<br />`
Dec 9`<br />`
Dec 24`<br />`

有人可以帮助我吗……谢谢:)

Could anybody help me... thanks :)

推荐答案

尝试一下



  1. 查找月份的第一天

  2. 按照Lalji Nakum的指示查找每月的最后一天

  3. 用每月的第15天检查日期,例如下面的例子




if($today < $hDay){
    echo 'Start : '. date("t-m-Y");
}else if ($today >= $hDay){
    echo 'Start :'. $hDay;
}




此处
1。$今天将是当前日期,
2。$ hDay将是该月的第15天,如11月15日

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

示例代码

echo 'First day of month '.
$fDay = date('01-m-Y');
echo '<br> 15th day of month '.
$hDay = date('d-m-Y', (strtotime($fDay)+ (86400 * 15)));
echo '<br> Last Day of month '.
$lDay = date("t-m-Y");

echo '<br> Current day '.
$today = date('d-m-Y');
//$today =  date('d-m-Y', strtotime($hDay)+86400 );
echo '<br>'; 
if($today < $hDay){
    echo 'Start : '. $lDay = date("t-m-Y");
}else if ($today >= $hDay){
    echo 'Start :'. $hDay;
}

这篇关于如何在PHP中获得每月的第15天和最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆