计算本周星期一的日期 [英] Calculate date for Monday of current week

查看:71
本文介绍了计算本周星期一的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:如果星期几不是星期一,则显示当前星期几的日期.如果星期几是星期一,只需显示今天的日期即可.

Goal: If current day of week is any day other than Monday, display the date of the Monday of the current week. If the current day of the week is Monday, simply display today's date.

**这是我写的,我认为可以,但是可能不是确定日期的最干净的方法.话虽这么说,有没有人看到代码错误或行不通的任何原因?**

** This is what I wrote and I think it works but is probably not the cleanest way to determine the date. Having said that, does anyone see any reason why the code would be wrong or not work? **

<?php
date_default_timezone_set("America/New_York");
$day = date("w");
if( $day == 1 ) {$day -= 0;}
if( $day == 2 ) {$day -= 1;}
if( $day == 3 ) {$day -= 2;}
if( $day == 4 ) {$day -= 3;}
if( $day == 5 ) {$day -= 4;}
if( $day == 6 ) {$day -= 5;}
if( $day == 0 ) {$day -= 6;}
$calc = mktime(0,0,0,date("m"),date("d")-$day,date("Y"));
echo date("m/d/Y",$calc)."<br>";
echo "start date of work week is: ".date("m/d/Y", $calc);
?>

推荐答案

是否愿意?

echo date("d m Y",strtotime('monday this week'));

这篇关于计算本周星期一的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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