如何从当月到周五

时间:2012-09-24 08:45:45

标签: php

我有一个问题,

$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'));

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

8 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分: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...

答案 3 :(得分:0)

本月将有5个星期五,只有它有30天,第一个星期五是一个月的第一天或第二天,或者它是31天,第一个星期五是一个月的第一天,第二天或第三天,所以你可以根据第五个星期五的计算做出条件陈述

答案 4 :(得分: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?

答案 5 :(得分:0)

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

答案 6 :(得分: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个,这似乎是用原始问题作为我的基础。

答案 7 :(得分:0)

<?php //php 7.0.8

$offDays = array();

$date = date('2020-02');
$day= 'Friday';
$offDays[0] = date('d',strtotime("first {$day} of ".$date));
$offDays[1] = $offDays[0] + 7;
$offDays[2] =  $offDays[0] + 14;
$offDays[3] =  $offDays[0] + 21;
$offDays['last'] = date('d',strtotime("last {$day} of ".$date));

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

foreach($offDays as $off){
    echo date('Y-m-d-D',strtotime(date($date."-".$off)));
    echo "\n";
}

?>