下个月,PHP当天 [英] Next month, same day in PHP

查看:97
本文介绍了下个月,PHP当天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个事件需要在每个月的同一天安排。

I have an "event" that needs to be scheduled the same day of every month.

假设你可以在5月1日之前设置开始日期,你应该在7月1日6月1日之后获得下一个活动。问题出现在31日(根据这个月份可能是30或28日)。

Say you set the start date on the 1st May you should get the next events on the 1st of Jun, 1 Jul etc. The problem comes with a start date on the 31st (the next ones could be 30 or 28 depending on the month).

考虑到有不同天数(28,30,31)的月份取决于月份本身和年份...这将是一个简单的方法来设置这个?

Considering that there are months with different numbers of days (28, 30, 31) depending on the month itself and the year... what would be an easy way to setup this?

考虑以下(和有缺陷的)nextmonth函数:

Consider the following (and flawed) nextmonth function:

$events = array()

function nextmonth($date) {
   return $date+(60*60*24*30);
}
$curr = $start;
while($curr < $end) {
    $events[ = $curr;
    $curr = nextmonth($curr);
}

编辑添加:对我来说的问题是,只要够解决任何给定月份的天数是多少,从而得到下一个相应的日期。

Edited to add: The problem for me is, simply enough, how to solve what the number of days of any given month is and thus get the next corresponding date.

推荐答案

更新: strong>

Update:

这将给你给定月份的天数:

This will give you the number of days in given month:

echo date('t', $timestamp);

请参阅: date()

旧答案

我不确定您想要的算法,但我相信这将有助于您:

I'm not sure about the algorithm you're thinking of but I believe this will help you:

echo date('d-M-y', strtotime('next month'));

这篇关于下个月,PHP当天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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