Important alert: (current site time 7/15/2013 10:41:22 AM EDT)
 

VB icon

mxHMS

Email
Submitted on: 1/23/2004 11:26:38 PM
By: Martyn M  
Level: Intermediate
User Rating: Unrated
Compatibility: PHP 4.0
Views: 7924
author picture
 
     This code allows you to convert seconds into a valid Hr:Min:Sec value. With fully customisable output features.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: mxHMS
// Description:This code allows you to convert seconds into a valid Hr:Min:Sec value. With fully customisable output features.
// By: Martyn M
//
// Inputs:Various settings, read the script comments.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1248&lngWId=8//for details.//**************************************

<?
/*
 * mxHMS
 * Seconds to customisable HH:MM:SS value
 *
 * Please read the comments in the function
 * and set it up how you wish!
 *
 * Used as a function:
 * $output = mxhms($seconds)
 *
 * The defaults would produce values:
 *
 *13 sec > 00m13s
 *62 sec > 01m02s
 * 4321 sec > 1h12m01s
 *
 *
 * v1.00 24 Jan 04
 *
 */ 
function mxHMS($sec)
{
 // what prefixes to use
 $prefix['hr'] = "h";
 $prefix['mi'] = "m";
 $prefix['se'] = "s";
 // keep values visible even if they are zero
 $persist['hr'] = 0;
 $persist['mi'] = 1;
 
 // show leading zeroes for <10 (eg. 03 vs 3)
 $lzero['hr'] = 0;
 $lzero['mi'] = 1;
 $lzero['se'] = 1;
 // calulate time
 $hr = floor(($sec / 3600));
 $mi = floor(($sec / 60) - ($hr * 60));
 $se = floor(($sec - (($hr * 3600) + ($mi * 60))));
 
 // first parse (visible & prefix)
 $hr = ($hr > 0 ? $hr.$prefix['hr'] : ($persist['hr'] ? $hr.$prefix['hr'] : ""));
 $mi = ($mi > 0 ? $mi.$prefix['mi'] : ($persist['mi'] ? $mi.$prefix['mi'] : ""));
 $se = ($se > 0 ? $se.$prefix['se'] : "");
 
 // second parse (leading zero)
 $hr = ((!empty($hr) && $hr < 10 && $lzero['hr']) ? "0".$hr : $hr);
 $mi = ((!empty($mi) && $mi < 10 && $lzero['mi']) ? "0".$mi : $mi);
 $se = ((!empty($se) && $se < 10 && $lzero['se']) ? "0".$se : $se);
 return $hr.$mi.$se;
}
// this is just so you can see it working.
echo mxHMS(4321);
?>


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/23/2004 11:30:02 PM

This is a WIP.

I am already thinking of addings a 'day' value if the seconds go over 86400. Maybe even a full duration() function like on IRC!
(If this comment was disrespectful, please report it.)

 
4/2/2004 9:40:24 AMBerndSoft

mktime();
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.