All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Convert Seconds Into Minutes And Hours Using PHP

Last Updated : Jul 1, 2023

IN - PHP HTML | Written & Updated By - Anjali

In this tutorial we will show you how to convert seconds into minutes and hours using PHP. You may also like convert bytes intp kb, mb and gb using PHP.

Convert Seconds Into Minutes And Hours Using PHP

To Convert Seconds It Takes Only two Steps:-

  1. Make a PHP file and define markup and scripting
  2. Make a CSS file and define styling

Step 1. Make a PHP file and define markup and scripting

We make a PHP file and save it with a name convert.php

<?php
if(isset($_POST['convert_seconds']))
{
 $time=$_POST['time'];
 $unit=$_POST['convert_unit'];
 if($unit == "Minutes")
 {
  $time = round($time / 60,4) . 'Min';
 }
 if($unit == "Hours")
 {
  $time = round($time / 60 / 60,4) . 'Hours';	
 }
}
?>

<html>
<head>
<link type="text/css" rel="stylesheet" href="convert_style.css"/>
</head>
<body>
<div id="wrapper">

<div id="convert_div">
 <form method="post"action="">
  <input type="text" name="time" placeholder="Enter Seconds">
  <select name="convert_unit">
   <option>Minutes</option>
   <option>Hours</option>
  </select>
  <br>
  <input type="submit" name="convert_seconds" value="Convert Seconds">
 </form>
 <p><?php echo $time;?></p>
</div>

</div>
</body>
</html>

In this step we create a form for user to enter seconds and select which unit he wants seconds to convert and after submitting the form we get the value and convert unit and convert seconds to desirable unit then display the time.

You may also like convert HTML into PDF.

Step 2. Make a CSS file and define styling

We make a CSS file and save it with a name convert_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#F2F2F2;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#424242;
}
#wrapper h1 p
{
 font-size:18px;
}
#convert_div input[type="text"]
{
 width:400px;
 height:55px;
 padding-left:10px;
 font-size:18px;
 margin-bottom:15px;
 color:#424242;
 font-weight:bold;
}
#convert_div select
{
 margin:0px;
 padding:0px;
 width:100px;
 height:55px;
 font-size:15px;
 margin-bottom:15px;
 color:#424242;
 font-weight:bold;
}
#convert_div input[type="submit"]
{
 width:330px;
 height:45px;
 font-size:16px;
 font-weight:bold;
 background-color:#6E6E6E;
 color:white;
 border:none;
 box-shadow:0px 4px 0px 0px #585858;
 border-radius:3px;
}

That's all, this is how to convert seconds into minutes and hours using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on convert time to hours helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪