[英]PHP Carbon nth days of the month
2018年1月19日是该月的第三个星期五。
我想要数字3表示这是第三个星期五。 Carbon可以这样做吗?
此外,我将使用它来检查另一个日期是否在另一个月的第三个星期五之前或之后。 例如,2018年2月17日在2月的第三个星期五之前还是之后?
Carbon是PHP类DateTime之上的一层,它具有非常强大的功能,因为它支持此处概述的许多格式,特别是您在寻找相对格式 。
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类中有任何功能可以允许第一部分采用单一的DateTime格式,但是,您可以使用如下所示的内容:
例如:
$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";
显然可以。
编辑:忽略此
我不认为Carbon可以完全满足您的直接要求,但是使用内置的吸气剂进行一些计算,我相信您可以轻松实现这种安静。
http://carbon.nesbot.com/docs/#api-getters
首先解析日期:
$dt = Carbon::parse('2018-19-1 23:26:11.123789');
然后使用weekOfMonth
, dayOfMonth
, dayOfWeek
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.