Blog

In this totaling I am going to portion fast PHP Function to Convert Seconds into Years, Months, Days, Hours, Minutes and Seconds. Itll convert quantity number of seconds into human readable format Years, Months, Days, Hours, Minutes and Seconds (eg: 246395678 seconds = 7 Years, 9 months, 21 days, 19 hours, 14 minutes and 38 seconds). After PHP 5.3 general pardon you dont dependence to manually convert seconds into supplementary readable format. There is a predefined performance in php called date_diff to convert seconds into Years, Months, Days, Hours, Minutes and Seconds.

Just copy and presenter knocked out court stroke in your PHP page.

<?php
function convertSecToTime($sec) 
 {
  $date1 = new DateTime("@0");
  $date2 = new DateTime("@$sec");
  $interval =  date_diff($date1, $date2);
  return $interval->format('%y Years, %m months, %d days, %h hours, %i minutes and %s seconds');
  // convert into Days, Hours, Minutes
  // return $interval->format('%a days, %h hours, %i minutes and %s seconds'); 
  }
?>

After that call convertSecToTime($sec) and pass sum number of seconds which obsession to convert into Years, Months, Days, Hours, Minutes and Seconds.

echo convertSecToTime(246395678);
//OUTPUT: 7 Years, 9 months, 21 days, 19 hours, 14 minutes and 38 seconds

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.