且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建一个函数,可以找出上个月的最后一天的日期?

更新时间:2023-11-23 21:15:40

假设 $ day 是UNIX时间戳记:

Assuming $day is a UNIX timestamp:

// gets you the first of the month
$date = getdate(mktime(0, 0, 0, date('n', $day), 1, date('Y', $day)));

从这里,执行以下操作之一:

From here, do one of these:

$lastDayOfPrevMonth = strtotime('-1 day', $date[0]);

// or, better, get the first day "in the grid" (assuming week starts on Sunday)
$date = strtotime("-$date[wday] days", $date[0]);

我不想竖琴,但是你看了我的上一个答案?这是一个更可靠的方法来构建日历,而不是所有这种特殊情况检查。

I don't want to harp on it, but have you looked at my previous answer? That's a more robust way to build a calendar instead of all this special case checking.