本周(星期一)和星期五获取当天(PHP) [英] Get date for monday and friday for the current week (PHP)

查看:137
本文介绍了本周(星期一)和星期五获取当天(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.

这篇关于本周(星期一)和星期五获取当天(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆