|——
应用中会有各种不同的需求,要灵活应对:比如拿到某一日期要知道是周几
|——DAYOFWEEK(date) 【返回日期date的星期索引(1=星期天,2=星期一, ……7=星期六)。这些索引值对应于ODBC标准。】
mysql> select DAYOFWEEK(CURRENT_TIMESTAMP());
+--------------------------------+
| DAYOFWEEK(CURRENT_TIMESTAMP()) |
+--------------------------------+
| 3 |
+--------------------------------+
1 row in set
没错就是3 对应ODBC标准中找出对应 就是星期二;好坑。
写了一函数转换:
private function _week($num)
{
$res = '';
switch ($num) {
case 1:
$res = '星期天';
break;
case 2:
$res = '星期一';
break;
case 3:
$res = '星期二';
break;
case 4:
$res = '星期三';
break;
case 5:
$res = '星期四';
break;
case 6:
$res = '星期五';
break;
case 7:
$res = '星期六';
break;
}
return $res;
}
|——WEEKDAY(date)【返回date的星期索引(0=星期一,1=星期二, ……6= 星期天)。】
mysql> select WEEKDAY(CURRENT_TIMESTAMP());
+------------------------------+
| WEEKDAY(CURRENT_TIMESTAMP()) |
+------------------------------+
| 1|
+------------------------------+
1 row in set
怎么又是1了 好坑。
|——DAYOFMONTH(date) 【返回date的月份中日期,在1到31范围内。】
mysql> select DAYOFMONTH('2008-08-08');
+--------------------------+
| DAYOFMONTH('2008-08-08') |
+--------------------------+
| 8 |
+--------------------------+
1 row in set
|——DAYOFYEAR(date) 【返回date在一年中的日数, 在1到366范围内。】
mysql> select DAYOFYEAR('2008-08-08');
+-------------------------+
| DAYOFYEAR('2008-08-08') |
+-------------------------+
| 221 |
+-------------------------+
1 row in set
|——MONTH(date)【返回date的月份,范围1到12。】
mysql> select MONTH('2008-08-08');
+---------------------+
| MONTH('2008-08-08') |
+---------------------+
| 8 |
+---------------------+
1 row in set
|——DAYNAME(date)【返回date的星期名字。】
mysql> select DAYNAME("2008-08-08");
+-----------------------+
| DAYNAME("2008-08-08") |
+-----------------------+
| Friday |
+-----------------------+
1 row in set
|——MONTHNAME(date)【返回date的月份名字。】
mysql> select MONTHNAME("2008-08-08");
+-------------------------+
| MONTHNAME("2008-08-08") |
+-------------------------+
| August |
+-------------------------+
1 row in set
|——QUARTER(date) 【返回date一年中的季度,范围1到4。】
mysql> select QUARTER("2008-08-08");
+-----------------------+
| QUARTER("2008-08-08") |
+-----------------------+
| 3 |
+-----------------------+
1 row in set
|——WEEK(date) | WEEK(date,first)
对于星期天是一周的第一天的地方,有一个单个参数,返回date的周数,范围在0到52。2个参数形式WEEK()允许
你指定星期是否开始于星期天或星期一。如果第二个参数是0,星期从星期天开始,如果第二个参数是1,
从星期一开始。
mysql> select week('2008-08-08',1);
+----------------------+
| week('2008-08-08',1) |
+----------------------+
| 32 |
+----------------------+
1 row in set
mysql> select week('2008-08-08',0);
+----------------------+
| week('2008-08-08',0) |
+----------------------+
| 31 |
+----------------------+
1 row in set
|——YEAR(date)【返回date的年份,范围在1000到9999。】
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| 2014 |
+-------------+
1 row in set
|——CURRENT_DATE【以'YYYY-MM-DD'或YYYYMMDD格式返回今天日期值,取决于函数是在一个字符串还是数字上下文被使用。】
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2014-04-15 |
+------------+
1 row in set
mysql> select curdate() + 0;
+---------------+
| curdate() + 0 |
+---------------+
| 20140415 |
+---------------+
1 row in set
|——CURTIME() | CURRENT_TIME 【以'HH:MM:SS'或HHMMSS格式返回当前时间值,取决于函数是在一个字符串还是在数字的上下文被使用。】
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 15:11:00 |
+-----------+
1 row in set
mysql> select current_time;
+--------------+
| current_time |
+--------------+
| 15:11:13 |
+--------------+
1 row in set
mysql> select current_time + 0;
+------------------+
| current_time + 0 |
+------------------+
| 151157.000000 |
+------------------+
1 row in set
|—— NOW() == SYSDATE() == CURRENT_TIMESTAMP
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2014-04-15 15:12:58 |
+---------------------+
1 row in set
mysql> select now() + 0;
+-----------------------+
| now() + 0 |
+-----------------------+
| 20140415151306.000000 |
+-----------------------+
1 row in set
参考:
http://www.corange.cn/archives/2008/06/1275.html Mysql 日期转换星期