I have a problem,
$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'));
but there is no fifth friday. Some months have fifth fridays. How to check and not set the last item array?
$fifth = strtotime('fifth friday of this month');
if (date('m') === date('m', $fifth)) {
$fridays[4] = date('Y-m-d', $fifth);
}
You can do this using the PHP date function. Get the month you want in $timestamp
and then do something like this:
<?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');
You can find a similar post about this: In PHP, how to know how many mondays have passed in this month uptil today?
I have a function to count fridays in a month to my very own application.... it could be helpful for someone.
function countFridays($month,$year){ $ts=strtotime('first friday of '.$year.'-'.$month.'-01'); $ls=strtotime('last day of '.$year.'-'.$month.'-01'); $fridays=array(date('Ym-d', $ts)); while(($ts=strtotime('+1 week', $ts))<=$ls){ $fridays[]=date('Ym-d', $ts); }return $fridays; }
I'm not on a machine that I could test this but what about something along the lines of....
if(date('Y-m-d', strtotime('fifth friday of this month')) > ""){
$fridays[4] = date('Y-m-d', strtotime('fifth friday of this month'));
}
The link below does exactly the same thing and will cover exactly what you are wanting todo without clunky if statements like above..
VERY Similar reading: read more...
只有当月有 30 天且第一个星期五是该月的第一天或第二天,或者如果有 31 天且第一个星期五是该月的第一天、第二天或第三天时,该月才会有 5 个星期五,因此您可以进行条件语句根据第 5 个星期五的计算
for($i=0;$i<=5;$i++)
{
echo date("d/m/y", strtotime('+'.$i.' week friday september 2012'));
}
I used this as the basis for my own solution:
$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);
I needed to get an array of every Friday in a month, even if there were 5 and this seemed to do the trick using the original question as my basis.
<?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";
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.