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

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

问题描述

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

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

假设你设置1日开始日期愿你应该得到下一个事件上月1日,7月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.

推荐答案

更新:

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

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

echo date('t', $timestamp);

请参阅:日起()

旧的回答:

我不知道你想的算法,但我相信这将帮助你:

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天全站免登陆