Функция MySQL YEARWEEK ()
Функция YEARWEEK ()
MySQL YEARWEEK () возвращает номер года и недели для указанной даты.
Синтаксис:
YEARWEEK ()
Синтаксическая диаграмма:
Версия MySQL: 5.6
Видео презентация
Иллюстрированная презентация
Пример: функция MySQL YEARWEEK ()
Следующее утверждение вернет номер года и недели даты 2009-05-18.
Код:
SELECT YEARWEEK('2009-05-18');
Пример вывода:
mysql> SELECT YEARWEEK ('2009-05-18'); + ------------------------ + | ГОД (2009-05-18) | + ------------------------ + | 200920 | + ------------------------ + 1 ряд в наборе (0,00 сек)
PHP скрипт
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-yearweek-function - php mysql examples | w3resource</title>
<meta name="description" content="example-yearweek-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Year and week number of the specified date 2009-05-18:</h2>
<table class='table table-bordered'>
<tr>
<th>Year and week number of the specified date 2009-05-18</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT YEARWEEK("2009-05-18")') as $row) {
echo "<tr>";
echo "<td>" . $row['YEARWEEK("2009-05-18")'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
JSP скрипт
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-yearweek-function</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host ="jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT YEARWEEK('2009-05-18')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Year and week number of the specified date 2009-05-18</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("YEARWEEK('2009-05-18')")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>
Пример: YEARWEEK () использует другой день той же недели
Следующее утверждение вернет год и неделю для дат 2007-12-31 и 2008-01-01.
Код:
SELECT YEARWEEK('2007-12-31'),
YEARWEEK('2008-01-01');
Пример вывода:
mysql> SELECT YEARWEEK ('2007-12-31'), YEARWEEK ('2008-01-01'); + ------------------------ + ------------------------ + | ГОД (2007-12-31) | ГОД (2008-01-01) | + ------------------------ + ------------------------ + | 200752 | 200752 | + ------------------------ + ------------------------ + 1 ряд в наборе (0,00 сек)
PHP скрипт
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-yearweek-function - php mysql examples | w3resource</title>
<meta name="description" content="example-yearweek-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Year and week of specified dates 2007-12-31 and 2008-01-01:</h2>
<table class='table table-bordered'>
<tr>
<th>Year and week of 2007-12-31</th><th>Year and week of 2008-01-01</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT YEARWEEK("2007-12-31"),YEARWEEK("2008-01-01")') as $row) {
echo "<tr>";
echo "<td>" . $row['YEARWEEK("2007-12-31")'] . "</td>";
echo "<td>" . $row['YEARWEEK("2008-01-01")'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
Все функции даты и времени:
Нажмите здесь, чтобы увидеть функции даты и времени MySQL.
Предыдущая: НЕДЕЛЯ ГОДА ()
Далее: Функции шифрования и сжатия MySQL AES_DECRYPT ()
Новый контент: Composer: менеджер зависимостей для PHP , R программирования
disqus2code