Skip to content

Instantly share code, notes, and snippets.

@OkoyaUsman
Created February 3, 2018 11:42
Show Gist options
  • Save OkoyaUsman/8e2e1224cc3fb1f2c82c0d383ad67240 to your computer and use it in GitHub Desktop.
Save OkoyaUsman/8e2e1224cc3fb1f2c82c0d383ad67240 to your computer and use it in GitHub Desktop.
PHP Function to Convert Seconds into Years, Months, Days, Hours, Minutes and Seconds.
<?php
function convertSecToTime($sec){
$date1 = new DateTime("@0"); //starting seconds
$date2 = new DateTime("@$sec"); // ending seconds
$interval = date_diff($date1, $date2); //the time difference
return $interval->format('%y Years, %m months, %d days, %h hours, %i minutes and %s seconds'); // 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
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment