English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP custom function to implement the method of formatting seconds

This article illustrates the method of implementing the format second function in PHP custom function. Shared for everyone's reference, as follows:

function vtime($time) {
  $output = '';
  foreach (array(86400 => 'Day', 3600 => 'Hour', 60 => 'Minute', 1 => 'Second') as $key => $value) {
    if ($time >= $key) $output .= floor($time/$key) . $value;
    $time %= $key;
  }
  if($output==''){
    $output=0;
  }
  return $output;
}
//$now=time();
$oldtime=86465;
//echo vtime($now);//Output:17058Day4Hour8Minute55Second
echo vtime($oldtime);//Output:1Day1Minute5Second

Readers who are interested in more PHP-related content can check out the special topics on this site: 'Summary of PHP Date and Time Usage', 'Summary of PHP Mathematical Operation Skills', 'Comprehensive Guide to PHP Array (Array) Operation Skills', 'PHP Basic Syntax Tutorial', 'Summary of PHP Operation and Operator Usage', 'PHP Object-Oriented Program Design Tutorial', 'Summary of PHP Network Programming Skills', 'Summary of PHP String (string) Usage', 'php+MySQL Database Operation Tutorial》and《Summary of Common PHP Database Operation Skills>

I hope the content described in this article will be helpful to everyone in PHP program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending an email) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like