本周(星期一)和星期五获取当天(PHP)

本周(星期一)和星期五获取当天(PHP)

问题描述:

如何获得本周的星期一和星期五的日期?

How can I get the date for monday and friday for the current week?

我有以下代码,但如果当天是星期日或星期六,则会失败。

I have the following code, but it fails if current day is sunday or saturday.

$current_day = date("N");
$days_to_friday = 5 - $current_day;
$days_from_monday = $current_day - 1;
$monday = date("Y-m-d", strtotime("- {$days_from_monday} Days"));
$friday = date("Y-m-d", strtotime("+ {$days_to_friday} Days"));


这些strtotime输入工作非常好:

These strtotime inputs work very well:

strtotime( "next monday" );
strtotime( "previous monday" );
strtotime( "today" );
strtotime( "next friday" );
strtotime( "previous friday" );

所有你需要做的是将逻辑包含在一些if语句中。

All you need to do is to wrap the logic inside some if statements.