php获取本周周一、周日时间,上周周一、周日时间,本月第一天,本月最后一天,上个月第一天,最后一天时间

本文介绍了一系列PHP函数用于获取特定日期(如本周星期一、本周星期天、上周星期一、上周星期天、本月第一天、本月最后一天、上月第一天、上月最后一天)的时间戳或日期格式,并提供了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//这个星期的星期一
// @$timestamp ,某个星期的某一个时间戳,默认为当前时间
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式
function this_monday($timestamp=0,$is_return_timestamp=true){
	static $cache ;
	$id = $timestamp.$is_return_timestamp;
	if(!isset($cache[$id])){
		if(!$timestamp) $timestamp = time();
		$monday_date = date('Y-m-d', $timestamp-86400*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-/*6*86400*/518400));
		if($is_return_timestamp){
			$cache[$id] = strtotime($monday_date);
		}else{
			$cache[$id] = $monday_date;
		}
	}
	return $cache[$id];

}

//这个星期的星期天
// @$timestamp ,某个星期的某一个时间戳,默认为当前时间
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式
function this_sunday($timestamp=0,$is_return_timestamp=true){
	static $cache ;
	$id = $timestamp.$is_return_timestamp;
	if(!isset($cache[$id])){
		if(!$timestamp) $timestamp = time();
		$sunday = this_monday($timestamp) + /*6*86400*/518400;
		if($is_return_timestamp){
			$cache[$id] = $sunday;
		}else{
			$cache[$id] = date('Y-m-d',$sunday);
		}
	}
	return $cache[$id];
}

//上周一
// @$timestamp ,某个星期的某一个时间戳,默认为当前时间
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式
function last_monday($timestamp=0,$is_return_timestamp=true){
	static $cache ;
	$id = $timestamp.$is_return_timestamp;
	if(!isset($cache[$id])){
		if(!$timestamp) $timestamp = time();
		$thismonday = this_monday($timestamp) - /*7*86400*/604800;
		if($is_return_timestamp){
			$cache[$id] = $thismonday;
		}else{
			$cache[$id] = date('Y-m-d',$thismonday);
		}
	}
	return $cache[$id];
}

//上个星期天
// @$timestamp ,某个星期的某一个时间戳,默认为当前时间
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式
function last_sunday($timestamp=0,$is_return_timestamp=true){
	static $cache ;
	$id = $timestamp.$is_return_timestamp;
	if(!isset($cache[$id])){
		if(!$timestamp) $timestamp = time();
		$thissunday = this_sunday($timestamp) - /*7*86400*/604800;
		if($is_return_timestamp){
			$cache[$id] = $thissunday;
		}else{
			$cache[$id] = date('Y-m-d',$thissunday);
		}
	}
	return $cache[$id];

}

//这个月的第一天
// @$timestamp ,某个月的某一个时间戳,默认为当前时间
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式

function month_firstday($timestamp = 0, $is_return_timestamp=true){
	static $cache ;
	$id = $timestamp.$is_return_timestamp;
	if(!isset($cache[$id])){
		if(!$timestamp) $timestamp = time();
		$firstday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp),1,date('Y',$timestamp)));
		if($is_return_timestamp){
			$cache[$id] = strtotime($firstday);
		}else{
			$cache[$id] = $firstday;
		}
	}
	return $cache[$id];
}

//这个月的第一天
// @$timestamp ,某个月的某一个时间戳,默认为当前时间
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式

function month_lastday($timestamp = 0, $is_return_timestamp=true){
	static $cache ;
	$id = $timestamp.$is_return_timestamp;
	if(!isset($cache[$id])){
		if(!$timestamp) $timestamp = time();
		$lastday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp),date('t',$timestamp),date('Y',$timestamp)));
		if($is_return_timestamp){
			$cache[$id] = strtotime($lastday);
		}else{
			$cache[$id] = $lastday;
		}
	}
	return $cache[$id];
}

//上个月的第一天
// @$timestamp ,某个月的某一个时间戳,默认为当前时间
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式

function lastmonth_firstday($timestamp = 0, $is_return_timestamp=true){
	static $cache ;
	$id = $timestamp.$is_return_timestamp;
	if(!isset($cache[$id])){
		if(!$timestamp) $timestamp = time();
		$firstday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp)-1,1,date('Y',$timestamp)));
		if($is_return_timestamp){
			$cache[$id] = strtotime($firstday);
		}else{
			$cache[$id] = $firstday;
		}
	}
	return $cache[$id];
}

//上个月的第一天
// @$timestamp ,某个月的某一个时间戳,默认为当前时间
// @is_return_timestamp ,是否返回时间戳,否则返回时间格式

function lastmonth_lastday($timestamp = 0, $is_return_timestamp=true){
	static $cache ;
	$id = $timestamp.$is_return_timestamp;
	if(!isset($cache[$id])){
		if(!$timestamp) $timestamp = time();
		$lastday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp)-1, date('t',lastmonth_firstday($timestamp)),date('Y',$timestamp)));
		if($is_return_timestamp){
			$cache[$id] = strtotime($lastday);
		}else{
			$cache[$id] =  $lastday;
		}
	}
	return $cache[$id];
}
echo '本周星期一:'.this_monday(0,false).'';
echo '本周星期天:'.this_sunday(0,false).'';
echo '上周星期一:'.last_monday(0,false).'';
echo '上周星期天:'.last_sunday(0,false).'';
echo '本月第一天:'.month_firstday(0,false).'';
echo '本月最后一天:'.month_lastday(0,false).'';
echo '上月第一天:'.lastmonth_firstday(0,false).'';
echo '上月最后一天:'.lastmonth_lastday(0,false).'';

### 使用 PHP 和 SQL 计算本月最后一天是周几 #### PHP 实现方法 为了获得当前月份最后一天对应的星期数,在PHP中可以利用 `date` 函数以及 `strtotime` 来实现。通过设置时间为该月最后一秒再加一秒即进入下一个月的第一天,随后回退到前一天来得到这个月的最后一日,并最终求得其为哪一日。 ```php $lastDayOfMonth = strtotime("last day of this month"); $dayOfWeek = date('l', $lastDayOfMonth); echo "本月最后一天是:" . $dayOfWeek; ``` 上述代码片段展示了如何使用 PHP 的内置函数找出给定月份结束之日的具体名称[^1]。 对于更精确的结果返回数值形式表示的星期几(0代表星期天),可修改输出部分如下: ```php $numericDayOfWeek = date('N', $lastDayOfMonth); // ISO-8601 星期编号 (1=Monday, ..., 7=Sunday) // 如果希望按照 Sunday as the start of week,则调整如下: $sundayStartWeekNumber = ($numericDayOfWeek == 7 ? 0 : $numericDayOfWeek); echo "本月最后一天对应的是第 {$sundayStartWeekNumber} 周"; ``` 这段脚本会给出一个整型值指出具体某个月份终结于那一周中的何者。 #### MySQL 查询语句 而在MySQL数据库环境中执行相同操作则可以通过查询命令完成: ```sql SELECT DAYOFWEEK(LAST_DAY(CURDATE())) AS last_day_of_week_number, DATE_FORMAT(LAST_DAY(CURDATE()), '%W') AS last_day_name; ``` 这里采用了 `LAST_DAY()` 函数找到指定日期所在月份的最大日子;接着运用 `DAYOFWEEK()` 得知这一天落在星期内的位置——其中周一至周五分别映射成数字2~6而周六和周日分别是1与7;另外还借助了 `DATE_FORMAT()` 提供更加直观的文字描述[^4]. #### Oracle 数据库处理方式 针对Oracle环境下的解决方案同样简洁明了: ```sql SELECT TO_CHAR(LAST_DAY(SYSDATE), 'D') AS numeric_day_of_week, TO_CHAR(LAST_DAY(SYSDATE), 'Day') AS weekday_name FROM dual; ``` 此SQL语句组合了 `TO_CHAR()` 转换格式化输出、`LAST_DAY()` 定位每月最后一个有效工作日的功能,从而实现了目标需求[^5].
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值