How do I find what day the last saturday of the current month is in PHP?

白昼怎懂夜的黑 提交于 2019-11-29 16:10:39

last saturday seems to be interpreted as "previous saturday" in some constellations.

This is what works for me on PHP 5.3 on Windows 7: Jump to next month's first day, and look for last saturday.

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

works for me even in the edge case that the first day of next month is a saturday.

Interestingly enough, strtotime("last saturday of this month") does exactly that.

See Supported Date and Time Formats for more, particularly the section on Relative Formats.

These days people use strtotime too liberally... if you want to be real hardcore (and reinvent the wheel a bit) you can use this piece of code I wrote for Moodle back in the day, which I guess has some novelty value. I copied it from here.

The last Sunday of April 2011 is find_day_in_month(-1, 0, 4, 2011), which means "start searching from the last day of April 2011 backwards and tell me when the first Sunday you find is".

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;

    }
}

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;

$lastSaturDay= date('Y-m-d', strtotime('last saturday of this month'));

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!