MySQL SEC_TO_TIME() function
SEC_TO_TIME() function
MySQL SEC_TO_TIME() returns a time value by converting the seconds specified in the argument. The return value is in hours, minutes and seconds. The range of the result is in the time data type.
Syntax:
SEC_TO_TIME(secnd);
Where secnd is seconds.
Syntax Diagram:

MySQL Version: 5.6
Video Presentation:
Pictorial Presentation:

Example: MySQL SEC_TO_TIME() function
The following statement will return a time value after converting the second's value 3610.
Code:
SELECT SEC_TO_TIME(3610);
Sample Output:
mysql> SELECT SEC_TO_TIME(3610); +-------------------+ | SEC_TO_TIME(3610) | +-------------------+ | 01:00:10 | +-------------------+ 1 row in set (0.02 sec)
PHP script:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example-SEC_TO_TIME-function - php mysql examples | w3resource</title>
<meta name="description" content="example-SEC_TO_TIME-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>Calculate time from 3610 seconds:</h2>
<table class='table table-bordered'>
<tr>
<th>Time from seconds</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 SEC_TO_TIME(3610)') as $row) {
echo "<tr>";
echo "<td>" . $row['SEC_TO_TIME(3610)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
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-sec_to_time-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 SEC_TO_TIME(3610)";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Time from seconds</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("SEC_TO_TIME(3610)")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>
Example: SEC_TO_TIME() function in numeric format
The following statement will return the time value after converting the seconds value specified in the argument to hours, minutes and seconds in numeric format.
Code:
SELECT SEC_TO_TIME(3610)+0;
Sample Output:
mysql> SELECT SEC_TO_TIME(3610); +-------------------+ | SEC_TO_TIME(3610) | +-------------------+ | 01:00:10 | +-------------------+ 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.0">
<title>example1-SEC_TO_TIME-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-SEC_TO_TIME-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>Calculate time from seconds:</h2>
<table class='table table-bordered'>
<tr>
<th>Time from seconds</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 SEC_TO_TIME(3610)+0') as $row) {
echo "<tr>";
echo "<td>" . $row['SEC_TO_TIME(3610)+0'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
All Date and Time Functions:
Click here to see the MySQL Date and time functions.
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook