PHP Carbon 一个月的第 n 天
PHP Carbon nth days of the month
2018 年 1 月 19 日是该月的第三个星期五。
我想要数字 3,表示这是第三个星期五。 Carbon 可以做到这一点吗?
此外,我将使用它来检查另一个日期是在另一个月的第三个星期五之前还是之后。比如2018年2月17日是二月的第三个星期五之前还是之后?
显然可以。
编辑:忽略这个
我认为 Carbon 不能完全满足您的直接要求,但使用内置的 getter 进行一些计算我相信您可以轻松实现这一目标。
http://carbon.nesbot.com/docs/#api-getters
从解析日期开始:
$dt = Carbon::parse('2018-19-1 23:26:11.123789');
然后使用weekOfMonth
、dayOfMonth
、dayOfWeek
等
碳是 PHP class DateTime, which is very powerful as it supports many formats that are outlined here, specifically you're looking for the Relative Formats.
之上的一层
Third friday of february
是一种有效的相对格式,因此,您可以这样做:
Carbon::parse('February 17th') > Carbon::parse('third friday of february');
这将产生 true
,而:
Carbon::parse('February 15th') > Carbon::parse('third friday of february');
将产生 false
.
你需要注意的是,这种方式构造时格式非常重要,例如,third friday in february
是无效的。
我认为 DateTime class 中没有任何功能允许第一部分采用单一 DateTime 格式,但是,您可以使用类似这样的东西:
- 确定所提供日期的日期
- 找到当月的第一天
- 比较(天数/7)你的日期和第一天
例如:
$date = Carbon\Carbon::parse('January 18 2018');
$firstDay = Carbon\Carbon::parse("first {$date->format('l')} of {$date->format('F')}");
$nth = $firstDay->diffInDays($date) / 7 + 1;
echo "{$date->format('Y-m-d')} is the #{$nth} {$date->format('l')} in this month";
2018 年 1 月 19 日是该月的第三个星期五。
我想要数字 3,表示这是第三个星期五。 Carbon 可以做到这一点吗?
此外,我将使用它来检查另一个日期是在另一个月的第三个星期五之前还是之后。比如2018年2月17日是二月的第三个星期五之前还是之后?
显然可以。
编辑:忽略这个
我认为 Carbon 不能完全满足您的直接要求,但使用内置的 getter 进行一些计算我相信您可以轻松实现这一目标。
http://carbon.nesbot.com/docs/#api-getters
从解析日期开始:
$dt = Carbon::parse('2018-19-1 23:26:11.123789');
然后使用weekOfMonth
、dayOfMonth
、dayOfWeek
等
碳是 PHP class DateTime, which is very powerful as it supports many formats that are outlined here, specifically you're looking for the Relative Formats.
之上的一层Third friday of february
是一种有效的相对格式,因此,您可以这样做:
Carbon::parse('February 17th') > Carbon::parse('third friday of february');
这将产生 true
,而:
Carbon::parse('February 15th') > Carbon::parse('third friday of february');
将产生 false
.
你需要注意的是,这种方式构造时格式非常重要,例如,third friday in february
是无效的。
我认为 DateTime class 中没有任何功能允许第一部分采用单一 DateTime 格式,但是,您可以使用类似这样的东西:
- 确定所提供日期的日期
- 找到当月的第一天
- 比较(天数/7)你的日期和第一天
例如:
$date = Carbon\Carbon::parse('January 18 2018');
$firstDay = Carbon\Carbon::parse("first {$date->format('l')} of {$date->format('F')}");
$nth = $firstDay->diffInDays($date) / 7 + 1;
echo "{$date->format('Y-m-d')} is the #{$nth} {$date->format('l')} in this month";