如何找到本月最后一个星期六的哪一天是PHP?

问题描述 投票:2回答:5

这适用于大多数月份,但例如,2011年4月有5个星期六,所以这将返回23而不是30。

$last_saturday = date("j", strtotime('Fourth Saturday'.date('F o')));
php date strtotime
5个回答
6
投票

在某些星座中,last saturday似乎被解释为“上个星期六”。

这对我在Windows 7上的PHP 5.3有用:跳到下个月的第一天,并查找上周六。

 $nextMonthStart = mktime(0,0,0,date('m')+1,1,date('Y'));
 $last_saturday = date("d.m.Y",strtotime("previous saturday", $nextMonthStart));

即使在下个月的第一天是星期六的边缘情况下,我也能为我工作。


9
投票

有趣的是,strtotime("last saturday of this month")确实如此。

有关更多信息,请参阅Supported Date and Time Formats,特别是关于Relative Formats的部分。


1
投票

这些天人们太过宽松地使用strtotime ...如果你想成为真正的铁杆(并重新发明轮子),你可以使用我在当天为Moodle写的这段代码,我想这有一些新奇的价值。我从here复制了它。

2011年4月的最后一个星期日是find_day_in_month(-1, 0, 4, 2011),意思是“从2011年4月的最后一天开始向后搜索并告诉我你找到的第一个星期天是什么时候”。

See it in action

/**
 * Calculate the number of days in a given month
 *
 * @param int $month The month whose day count is sought
 * @param int $year The year of the month whose day count is sought
 * @return int
 */
function days_in_month($month, $year) {
   return intval(date('t', mktime(12, 0, 0, $month, 1, $year)));
}


/**
 * ?
 *
 * @todo Document what this function does
 * @param int $startday Defines the day from which to start searching (absolute value) and the direction in which to search (sign)
 * @param int $weekday -1 if you don't want the function to match a specific weekday; 0 to 6 to match specific weekdays
 * @param int $month The month wherein the day is sought
 * @param int $year The year wherein the day is sought
 * @return int
 */
function find_day_in_month($startday, $weekday, $month, $year) {
    $daysinmonth = days_in_month($month, $year);

    if($weekday == -1) {
        // Don't care about weekday, so return:
        //    abs($startday) if $startday != -1
        //    $daysinmonth otherwise
        return ($startday == -1) ? $daysinmonth : abs($startday);
    }

    // From now on we 're looking for a specific weekday

    // Give "end of month" its actual value, since we know it
    if($startday == -1) {
        $startday = -1 * $daysinmonth;
    }

    // Starting from day $startday, the sign is the direction

    if($startday < 1) {

        $startday = abs($startday);
        $lastmonthweekday  = strftime('%w', mktime(12, 0, 0, $month, $daysinmonth, $year));

        // This is the last such weekday of the month
        $lastinmonth = $daysinmonth + $weekday - $lastmonthweekday;
        if($lastinmonth > $daysinmonth) {
            $lastinmonth -= 7;
        }

        // Find the first such weekday <= $startday
        while($lastinmonth > $startday) {
            $lastinmonth -= 7;
        }

        return $lastinmonth;

    }
    else {

        $indexweekday = strftime('%w', mktime(12, 0, 0, $month, $startday, $year));

        $diff = $weekday - $indexweekday;
        if($diff < 0) {
            $diff += 7;
        }

        // This is the first such weekday of the month equal to or after $startday
        $firstfromindex = $startday + $diff;

        return $firstfromindex;

    }
}

0
投票

https://github.com/briannesbitt/carbon

<?php
require 'vendor/autoload.php';

use Carbon\Carbon;

$date = Carbon::now();
$date = $date->lastOfMonth(Carbon::SATURDAY);
echo 'last saturday = '.$date->day;

-2
投票

$ lastSaturDay = date('Y-m-d',strtotime('本月最后一个星期六'));

© www.soinside.com 2019 - 2024. All rights reserved.