w3resource logo


MySQL SEC_TO_TIME function

MySQL SEC_TO_TIME() function

rating has average rating 6 out of 10. Total 1 users rated.

<<PreviousNext>>

Description

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(seconds);

Where seconds is seconds.

Example :

Code

SELECT SEC_TO_TIME(3610);

Explanation

The above statement will return a time value after converting the seconds value 3610.

Output

MySQL SEC_TO_TIME()

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-SEC_TO_TIME-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Calculate time from 3610 seconds : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Time from seconds</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT SEC_TO_TIME(3610)");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row["SEC_TO_TIME(3610)"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Example : SEC_TO_TIME() function in numeric format

Code

SELECT SEC_TO_TIME(3610)+0;

Explanation

The above statement will return the time value after converting the seconds value specified in the argument to hours, minutes and seconds in numeric format.

Output

MySQL SEC_TO_TIME() EXAMPLE

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example1-SEC_TO_TIME-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Calculate time from seconds : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Time from seconds</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT SEC_TO_TIME(3610)+0");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['SEC_TO_TIME(3610)+0'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html> 

View the example in browser

photo credit: Ben Dodson. Photo is used under creative Common License.

<<PreviousNext>>