gpt4 book ai didi

php - PHP 中每月的第二个星期六函数

转载 作者:可可西里 更新时间:2023-11-01 00:07:08 24 4
gpt4 key购买 nike

我找到了这段 PHP 代码 credit to creator并希望以不同的方式实现它:

目标是让代码自动调整以下语句为从现在到永远每个月的第二个星期六:


下一次成员(member) session :星期六, MONTH, DAY, YEAR,上午 11 点到中午。

如:“2011 年 2 月 12 日,星期六,上午 11 点到中午。”


我不是 PHP 大师,有人可以编辑它以使其工作吗?

<?php

function nextMeeting($nextMonth = false) {

$day=date("j");
$month=date("n");
$year=date("Y");

if ($nextMonth) {
$day=1;
if ($month == 12) {
$month=1;
$year++;
} else {
$month++;
}
}

$dayofweek=date("w");
$firstOfMonth=date("w",mktime(0, 0, 0, $month , 1, $year ));


// figure out what date is the second Saturday of the month
if ( $firstOfMonth > 0 ) {
$firstSunday= 8 - $firstOfMonth;
} else {
$firstSunday= 1;
}

$firstSundayDate=date("D",mktime(0, 0, 0, $month ,
$firstSunday, $year));

// figure out what date the third monday of the month is
if ( $firstOfMonth > 1) {
$offSet = 8 - $firstOfMonth;
} elseif ( $firstOfMonth == 0 ) {
$offSet=1;
} else {
$offSet=0;
}
$thirdMonday= 15 + $offSet;
$thirdMondayDate=date("D",mktime(0, 0, 0, $month ,
$thirdMonday, $year));

// lets see which of these dates now applies
if ($day <= $firstSunday) {
// we didn't miss the first meeting
$nextMeeting=$firstSunday;
$nextMeetingDate=mktime(0, 0, 0, $month ,
$nextMeeting, $year);
} elseif ( ($day > $firstSunday) && ($day <= $thirdMonday) ) {
// we missed the first meeting of the month, but can still make the second
$nextMeeting=$thirdMonday;
$nextMeetingDate=mktime(0, 0, 0, $month ,
$nextMeeting, $year);
} else {
// we need to wait until next month
$nextMeetingDate=nextMeeting(1);
$nextMeeting=nextMeeting(1);
}

return $nextMeetingDate;

}

$meeting=nextMeeting();

echo "Next membership meeting is on " . date('l dS \of F Y', $meeting);

?>

最佳答案

你可以节省大约 5000 行并试试这个吗

<?
echo date('Y-m-d', strtotime('second saturday of february 2011'));

编辑

好吧,我在评论里撒谎了,我写给你看

<?

$now=date("U");
$monthyear=date("F Y");
$secondsat=date('U', strtotime($monthyear.' second saturday'));
if ($now>$secondsat) {
$monthyear=date("F Y", "next month");
$secondsat=date('U', strtotime($monthyear.' second saturday'));
}

echo date("m/d/Y",$secondsat);

关于php - PHP 中每月的第二个星期六函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4880660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com