你的位置:
问答吧
-> PHP基础
-> 问题详情
MYSQL如何获得指定日期所在周周一的日期?
如何获得指定日期所在周周一的日期?比如如何获得本周周一的日期?
作者: 十月冷风
发布时间: 2007-06-07
原来所有系统里都是一周从周日开始算的,自己写了个函数
这个函数可以得到当前日期所在周的周日日期:lol
复制PHP内容到剪贴板
PHP代码:function getsunday(){
$year=date("Y");
$month=date("m");
$day=date("d");
$week=date("w");
$date_str=date("Y-m-d",mktime(0, 0, 0, $month, $day-$week, $year));
return $date_str;
}
作者: 十月冷风
发布时间: 2007-06-07
你这个是用PHP实现的,可以直接用MYSQL实现啊,
其中curdate是你所要查询的日期,得到周一的,周日用0
mysql> select subdate(curdate(),date_format(curdate(),'%w')-1);
+--------------------------------------------------+
| subdate(curdate(),date_format(curdate(),'%w')-1) |
+--------------------------------------------------+
| 2007-06-04 |
+--------------------------------------------------+
1 row in set (0.00 sec)
[ 本帖最后由 lmhllr 于 2007-6-7 15:48 编辑 ]
作者: lmhllr
发布时间: 2007-06-07
引用:原帖由 十月冷风 于 2007-6-7 15:42 发表
原来所有系统里都是一周从周日开始算的,自己写了个函数
这个函数可以得到当前日期所在周的周日日期:lol
[php]
function getsunday(){
$year=date("Y");
$month=date("m");
$day=date("d");
$week=date ...这个函数不错!
中国人的习惯总是以星期一为每周的第一天,而计算机是以星期天为每周的第一天
应该可以转换过来的吧?
思考一下...
作者: fly1983
发布时间: 2007-06-07
mysql的date_format有u和v可以格式化周一为第一天
作者: lmhllr
发布时间: 2007-06-07
array getdate ( [int timestamp] )
返回值是个关联数组.
"mday" Numeric representation of the day of the month 1 to 31
"wday" Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
"yday" Numeric representation of the day of the year 0 through 365
"weekday" A full textual representation of the day of the week Sunday through Saturday
"month" A full textual representation of a month, such as January or March January through December
$today = getdate();
print_r($today);
?>
The above example will output something similar to:
Array
(
[seconds] => 40
[minutes] => 58
[hours] => 21
[mday] => 17
[wday] => 2
[mon] => 6
[year] => 2003
[yday] => 167
[weekday] => Tuesday
[month] => June
[0] => 1055901520
)
$array = getdate();
$array['wday'] 就是一个星期的第几天了.算一下就可以求出星期一来.
$today = getdate();
$w = time() - ($today['wday'] - 1 )* 24 * 3600;
$wmonday = date("Y-m-d", $w);
echo $wmonday;
?>
[ 本帖最后由 angeljyt 于 2007-6-7 18:23 编辑 ]
作者: angeljyt
发布时间: 2007-06-07