2012-09-24 36 views
2

我有一个问题让所有的星期五,如何从当月

$fridays = array(); 
$fridays[0] = date('Y-m-d', strtotime('first friday of this month')); 
$fridays[1] = date('Y-m-d', strtotime('second friday of this month')); 
$fridays[2] = date('Y-m-d', strtotime('third friday of this month')); 
$fridays[3] = date('Y-m-d', strtotime('fourth friday of this month')); 
$fridays[4] = date('Y-m-d', strtotime('fifth friday of this month')); 

但没有第五个星期五。有些月份有第五个星期五。如何检查并不设置最后一个项目数组?

+0

他们有第五周五取决于年和月..这段代码只会今年运行这个月 – Baba

+1

尝试使用'last',而不是'fifth'。稍后检查第四名是否与第五名不一样。 – Crozin

+0

谢谢@Crozin就是这样。最后=第四。 – user1642439

回答

6
$fifth = strtotime('fifth friday of this month'); 

if (date('m') === date('m', $fifth)) { 
    $fridays[4] = date('Y-m-d', $fifth); 
} 
+0

非常感谢 – user1642439

0

我不认为我可以测试这一点,但怎么样的线沿线的东西的机器上....

if(date('Y-m-d', strtotime('fifth friday of this month')) > ""){ 
$fridays[4] = date('Y-m-d', strtotime('fifth friday of this month')); 
} 

以下链接不完全一样的东西,将覆盖你究竟是什么想待办事项而不笨拙,如果像上述声明..

非常类似阅读:read more...

+0

但第五个星期五输出我2012-10-05,意味着下个月 – user1642439

+0

看看链接 - 它会做你想要的。直到我已经发布,我才意识到,因此编辑的数量哈哈! – Chris

0

月份将有5个星期五,如果有30天,第一个星期五是本月1日或第2天,或者如果它有31天和第一天周五是第一次,每月的第2或第3天,这样可以使在此基础上计算5日星期五

0

条件语句可以使用PHP日期函数做到这一点。获取月份要在$timestamp,然后做这样的事情:

<?php 
function fridays_get($month, $stop_if_today = true) { 

$timestamp_now = time(); 

for($a = 1; $a < 32; $a++) { 

    $day = strlen($a) == 1 ? "0".$a : $a; 
    $timestamp = strtotime($month . "-$day"); 
    $day_code = date("w", $timestamp); 
    if($timestamp > $timestamp_now) 
     break; 
    if($day_code == 5) 
     @$fridays++; 

} 

return $fridays; 
} 

echo fridays_get('2011-02'); 

您可以找到有关这个类似的帖子:In PHP, how to know how many mondays have passed in this month uptil today?

0
for($i=0;$i<=5;$i++) 
{ 
     echo date("d/m/y", strtotime('+'.$i.' week friday september 2012')); 
} 
0

我用这个作为我自己的解决方案的基础:

$fridays = array(); 
$fridays[0] = date('d',strtotime('first fri of this month')); 
$fridays[1] = $fridays[0] + 7; 
$fridays[2] = $fridays[0] + 14; 
$fridays[3] = $fridays[0] + 21; 
$fridays['last'] = date('d',strtotime('last fri of this month')); 

if($fridays[3] == $fridays['last']){ 
    unset($fridays['last']); 
} 
else { 
    $fridays[4] = $fridays['last']; 
    unset($fridays['last']); 
} 

print_r($fridays); 

我需要在一个月内得到每个星期五的数组,即使有5个,这似乎是用原始问题作为我的基础。

1

我有一个函数来计算一个月的星期五给我自己的应用程序....它可能对某人有帮助。

function countFridays($month,$year){ 
 
\t $ts=strtotime('first friday of '.$year.'-'.$month.'-01'); 
 
\t $ls=strtotime('last day of '.$year.'-'.$month.'-01'); 
 
\t $fridays=array(date('Y-m-d', $ts)); 
 
\t while(($ts=strtotime('+1 week', $ts))<=$ls){ 
 
\t \t $fridays[]=date('Y-m-d', $ts); 
 
\t }return $fridays; 
 
}