本教程操作环境:windows7系统、php8.1版、dell g3电脑
php将指定日期转化为星期的方法
在php中,可以使用date()和strtotime()函数来进行转换。
(其实主要用date(),但该函数处理的对象是时间戳;因此我们需要用strtotime()函数先将日期转为时间戳,在交给date()处理。)
1、使用strtotime()函数将指定日期转为时间戳
strtotime() 函数可将英文文本描述的日期时间描述解析为 unix 时间戳
<?phpheader("content-type:text/html;charset=utf-8");$str1="2022-05-1";$str2="2022-05-11";$time1=strtotime($str1);$time2=strtotime($str2);echo "$str1 的时间戳: ".$time1."<br>";echo "$str2 的时间戳: ".$time2;?>
2、将获取的时间戳转为星期天数
date()可以格式化时间,配合格式化字符“n”获取星期天数
n:返回iso-8601 格式数字表示的星期中的第几天(php 5.1.0 新加),范围 1(表示星期一)到 7(表示星期天)
echo "$str1 是星期 ".date("n",$time1)."<br>";echo "$str2 是星期 ".date("n",$time2);
查看日历,看看是否正确。
推荐:《php视频教程》
以上就是php怎么将指定日期转化为星期的详细内容。