获取当前季度天数的简单方法?
PHP 提供了获取当月当前日期 (date('j')) 以及当年当前日期 (date('z')) 的方法。 有没有办法获取当前季度的当前日期?
现在,8 月 5 日,是第三季度的第 36 天。
如果没有标准的计算方法,是否有人有方便的算法(最好是基于 PHP 的)?
PHP provides ways to get the number of the current day of the month (date('j')) as well as the number of the current day of the year (date('z')). Is there a way to get the number of the current day of the current quarter?
So right now, August 5, it is day 36 of the third quarter.
If there is no standard way of calculating this, does anyone have a (prefereably PHP-based) algorithm handy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
怎么样:
How about:
我用以下方法编写了一个类。 享受。
I wrote a class with the following methods. Enjoy.
或者
or
您可以使用 Carbon 它具有 getFirstOf{Month,Year,Quarter}() 的简单修饰符
You can use Carbon it has easy modifiers for getFirstOf{Month,Year,Quarter}()
我们需要首先计算第一季度的日期,
接下来我们计算 http://php .net/manual/en/datetime.diff.php
We need to calculate the date of the first quarter first
next we calculate the http://php.net/manual/en/datetime.diff.php
假设您指的是日历季度(因为公司会计年度可以在一年中的任何月份开始),您可以依靠日期('z')来确定一年中的日期,然后保留一个简单的数组每个季度开始的那一天:
然后,根据当前的年份,您可以首先找到小于或等于该年份的最大开始日,然后减去。
请注意,根据闰年,您需要不同的数字。
Assuming you mean a calendar-quarter (because a company fiscal year can start in any month of the year), you could rely on the date('z') to determine the day-of-year, and then keep a simple array of the day each quarter starts on:
Then with the current day-of-year you can first locate the largest start-day that's less than or equal to the day-of-year, then subtract.
Note that you need different numbers depending on the leap year.
我注意到这个帖子有点超出了问题的范围,这是对许多谷歌搜索“Quarter”和“Quarter”的第一个回应。 其中有“PHP”。
如果您正在使用 ISO 组织标准(如果您正在开发商业应用程序,则应该这样做),那么这
是不正确的,因为 ISO 标准中一年的第一天可以是 12 月 30 日或 12 月 31 日。
相反,你应该使用这个:
I've noticed that this thread went a bit beyond the question, and it's the first response to many google searches with "Quarter" & "PHP" in them.
If you're working with the ISO standards of organization, which you should if you're doing a business app, then
Is NOT correct, because the first day of a year in the ISO standards, can be 30, or 31 December.
Instead, you should use this :