简体   繁体   English

如何确定星期几在一个月中的第一,第二,第三等发生?

[英]How to determine if day of week is 1st, 2nd, 3rd, etc occurrence in a month?

I need to determine what day of the week a given date is, and what occurrence of that day of week it is in the given date's month. 我需要确定给定日期是星期几,以及给定日期月份是星期几。

The 3rd Sunday in January, 2017 is the 15th. 2017年1月的第3个星期日是15号。 I know this by looking at a calendar. 我通过看日历知道这一点。 But given I only know the exact date, how can I figure out it's the 3rd Sunday programmatically? 但是,由于我只知道确切的日期,我如何以编程方式确定它是第三个星期日?

Determining the actually day of week is pretty easy by loading the date into PHP and formatting it to output the day: 通过将日期加载到PHP中并将其格式化以输出日期,从而确定实际的星期几非常容易:

$day_of_week = date('l', $timestamp);

It's the other part I can't quite figure out. 这是我无法弄清楚的另一部分。

Try this which works out the current day or you could set any other valid timestamp to $timestamp: 尝试使用此方法可以解决当前问题 ,也可以将其他有效时间戳记设置为$ timestamp:

<?php
date_default_timezone_set('UTC');
$timestamp = time();
$day_of_week = date('l', $timestamp);
$day_of_the_month = date('j', $timestamp);
$occurence = ceil($day_of_the_month / 7);
$suffix = 'th';

if($occurence == 3){
  $suffix = 'rd';
} else if($occurence == 2){
  $suffix = 'nd';
} else if($occurence == 1){
  $suffix = 'st';
}

print 'It is the '.$occurence.$suffix.' '.$day_of_week.' of this month';
?>

I see no better way than just looping throught the full month and counting which on them is sunday. 除了循环浏览整个月并指望星期几是一个更好的方法。 I would also recommend you to use a library like Carbon ( Carbon oficial docs ) 我也建议您使用像Carbon这样的库( Carbon oficial docs

So, your code should be doing: 因此,您的代码应该这样做:

  1. You collect you date 你收集你的日期

 $dt = Carbon::createFromDate(2012, 10, 6); 

  1. A Carbon date object has a property called "dayOfWeek", so no more hassle here Carbon日期对象具有名为“ dayOfWeek”的属性,因此这里不再麻烦

 if ($dt->dayOfWeek === Carbon::SATURDAY) { } 

  1. @CBroe is actually better. @CBroe实际上更好。 You can just just divide by 7 your week number (no decimals) and add 1 to the result, that's the ocurrence. 您只需将您的周数除以7(无小数),然后将1加到结果中即可。

PD: Sorry, I got no PHP editor nor access to my development PC PD:抱歉,我没有PHP编辑器,也没有访问我的开发PC的权限

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用碳查找一周中的每一天 [1st,2nd,3rd] 对于接下来的 15 天 - Find every [ 1st,2nd,3rd ] day of week using carbon For next 15 day PHP日期和时间,在一个月内获得第1周,第2周,第3周和第4周 - PHP Date and Time, getting the 1st, 2nd, 3rd, and 4th week in a month 在php中显示当前星期的1、2、3、4和5日 - Display the 1st, 2nd, 3rd, 4th, & 5th date of the current week in php 如何改变 <option>第3个<select>基于第二次变化<select> ,其中第二个是通过使用jquery ajax更改第一个下拉列表的值来绘制的 - How to change <option> of a 3rd <select> based on change in 2nd <select>, where 2nd is drawn by changing value of 1st dropdown using jquery ajax 从第2和第3个表中获取第1个表中匹配值的值 - Get the values from 2nd and 3rd table for the matched values in 1st table 在代码点火器中显示前10个结果,并显示第1,第2和第3个最高值 - Display top 10 results in codeigniter and display 1st, 2nd and 3rd top values 我想在考试成绩中显示第 1、2、3 位 - I want to show position 1st, 2nd, 3rd in exam result MySQL - mysql日期格式后缀(第1,第2,第3 ......) - MySQL - mysql date format suffix (1st, 2nd, 3rd…) 我只想要第一个索引的值,然后是第二个索引,然后是第三个索引。 如何通过循环迭代并动态获取值? - I want only values of 1st index then 2nd and then 3rd index. How can I iterate through this with a loop and get the values dynamically? 如何在第1列和第2列的交叉点上填充第3列结果,这些列都来自用户在下拉列表中选择的同一个表? - How can I populate a 3rd column result on intersections of 1st and 2nd columns all from the same table that are selected by the user on dropdown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM