PHP:平日给定一个月的数字 [英] PHP: getting weekdays numbers of a given month
问题描述
给定一个月和一个工作日,我需要建立一个可以检索星期一,星期二,星期三,星期四和星期五的日期号的功能。这个功能应该是在2012年9月的所有星期一,3月10日,17日和17日。 24
请注意,对我来说,平日1号是星期一,星期二,3是星期三,4星期四和5星期五。
到目前为止,我已经做了:获得今天的日期的一周的第一天(我发布以下功能)。但我不知道如何从这里简单地跟随,我已经有很多小时了,我怀疑有更好的方法来做到这一点。你可以告诉我怎么样?
function getFirstDayOfWeek($ date){
$ getdate = getdate($ date) ;
//我们星期一多少天?
switch($ getdate ['wday']){
case 0://我们在星期天
$ days = 6;
break;
默认值://任何其他日子
$ days = $ getdate ['wday'] - 1;
break;
}
$ seconds = $ days * 24 * 60 * 60;
$ monday = date($ getdate [0]) - $ seconds;
return $ monday;
}
感谢一吨
不是很聪明,但适用于你:
// sept。 2012
$ month = 9;
//通过月份循环
($ i = 1; $ i <= 31; $ i ++){
//给定月份时间戳
$ timestamp = mktime(0,0,0,$ month,$ i,2012);
//确保我们没有去下个月
if(date(n,$ timestamp)== $ month){
//循环中的当前日期
$ day = date(N,$ timestamp);
//如果这是介于1到5之间,平日,1 =星期一,5 =星期五
if($ day == 1 OR $ day< = 5){
//现在写下来
$ days [$ day] [] = date(j,$ timestamp);
}
}
}
//查看是否可以工作:)
print_r($ days);
Given a Month and a weekday, I need to build a function that can retrieve the day number of all Mondays, Tuesdays, Wednesdays, Thursdays and Fridays.
Let's say I give the function this month, September 2012 and weekday number 1. The function should retrieve all the Mondays in September 2012 which are: 3, 10, 17 and 24
Please note that to me weekday number 1 is Monday, number 2 Tuesday, 3 is Wednesday, 4 Thursday and 5 Friday.
So far I've have done: getting the first day of the week given today's date (I post the function below). But I don't know how to follow from here in a simple way, I've been many hours on it and I suspect there's a better way to do it. Can you please show me how?
function getFirstDayOfWeek($date) {
$getdate = getdate($date);
// How many days ahead monday are we?
switch ( $getdate['wday'] ) {
case 0: // we are on sunday
$days = 6;
break;
default: // any other day
$days = $getdate['wday']-1;
break;
}
$seconds = $days*24*60*60;
$monday = date($getdate[0])-$seconds;
return $monday;
}
Thanks a ton
Not very smart, but would works for you:
// sept. 2012
$month = 9;
// loop through month days
for ($i = 1; $i <= 31; $i++) {
// given month timestamp
$timestamp = mktime(0, 0, 0, $month, $i, 2012);
// to be sure we have not gone to the next month
if (date("n", $timestamp) == $month) {
// current day in the loop
$day = date("N", $timestamp);
// if this is between 1 to 5, weekdays, 1 = Monday, 5 = Friday
if ($day == 1 OR $day <= 5) {
// write it down now
$days[$day][] = date("j", $timestamp);
}
}
}
// to see if it works :)
print_r($days);
这篇关于PHP:平日给定一个月的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!