w3resource

MySQL LAST_DAY() function

LAST_DAY() function

MySQL LAST_DAY() returns the last day of the corresponding month for a date or datetime value. If the date or datetime value is invalid, the function returns NULL.

Syntax:

LAST_DAY(date1)

Where date1 is a date.

Syntax Diagram:

MySQL LAST_DAY() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL LAST_DAY() function

Example: MySQL LAST_DAY() function

The following statement will return the last date of the corresponding month of the given date 2009-05-18.

Code:

SELECT LAST_DAY('2009-05-18');

Sample Output:

mysql> SELECT LAST_DAY('2009-05-18');
+------------------------+
| LAST_DAY('2009-05-18') |
+------------------------+
| 2009-05-31             | 
+------------------------+
1 row in set (0.00 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-LAST_DAY-function - php mysql examples | w3resource</title>
<meta name="description" content="example-LAST_DAY-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>Last day of 2009-05-18:</h2>
<table class='table table-bordered'>
<tr>
<th>Last day of 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 LAST_DAY("2009-05-18")') as $row) {
echo "<tr>";
echo "<td>" . $row['LAST_DAY("2009-05-18")'] . "</td>"; 
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script

<%@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-last_day-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 LAST_DAY('2009-05-18')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Last day of 2009-05-18</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("LAST_DAY('2009-05-18')")%></TD>
</TR>

<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : LAST_DAY() function with datetime

The following statement will return the last date of the corresponding month of the given datetime 2009-02-18 15:45:53.

Code:

SELECT LAST_DAY('2009-02-18 15:45:53');

Sample Output:

mysql> SELECT LAST_DAY('2009-02-18 15:45:53');
+---------------------------------+
| LAST_DAY('2009-02-18 15:45:53') |
+---------------------------------+
| 2009-02-28                      | 
+---------------------------------+
1 row in set (0.00 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-LAST_DAY-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-LAST_DAY-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>Last day of 2009-02-18 15:45:53:</h2> <table class='table table-bordered'> <tr> <th>Last day of 2009-02-18 15:45:53</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 LAST_DAY("2009-02-18 15:45:53")') as $row) { echo "<tr>"; echo "<td>" . $row['LAST_DAY("2009-02-18 15:45:53")'] . "</td>"; echo "</tr>"; } ?> </tbody></table> </div> </div> </div> </body> </html>

View the example in browser

All Date and Time Functions :

Click here to see the MySQL Date and time functions.

Previous: HOUR()
Next: LOCALTIME()