sec_to_time(), sec_to_string(), string_to_sec(), time_diff()
By : voldomazta
<?
/**
* Time Functions
* by: Ramon Alivio Jr.
* on: 2005-06-17
**/
class Time {
/**
* Returns the time of the day using an integer argument which means the number of seconds from 12:00:00AM
* Examples : 32563 (09:02 AM), 53447 (2:50 PM)
**/
function sec_to_string($s,$f = '') {
$h = floor($s/3600);
$m = floor($s/60) - ($h * 60);
$ts = $s - ($h * 3600) - ($m * 60);
$hours = (strlen($h) == 1 ? '0'.$h : $h);
$minutes = (strlen($m) == 1 ? '0'.$m : $m);
$seconds = (strlen($ts) == 1 ? '0'.$ts : $ts);
$meridian = ' '.($h == 12 ? 'NN' : ($h > 12 ? 'PM' : 'AM'));
if($h > 24) return 'Invalid Time';
switch($f) {
case 12: return ($hours > 12 ? $hours-12 : $hours).':'.$minutes.$meridian;
case 24: return $hours.':'.$minutes; break;
default: return $hours.':'.$minutes.':'.$seconds; break;
}
}
/**
* A reverse of sec_to_string()
* Returns the number of seconds from 12:00:00AM using a string argument of the current time of the day.
* Example Arguments : 6:02:54PM (64974), 11:05AM (39900)
**/
function string_to_sec($time) {
preg_match("/([0-9]{0,2})\:([0-9]{0,2})\:?([0-9]{0,2})?\s?([a|A|p|P|n|N])([m|M|n|N])/",$time,$matches);
$hours = $matches[1];
$minutes = $matches[2];
$seconds = (empty($matches[3]) ? '00' : $matches[3]);
$meridian = strtoupper($matches[4].$matches[5]);
if($hours > 12 || $hours < 0) return false;
switch($meridian) {
case 'AM':
if($hours == 12) $hours = 0;
break;
case 'NN':
if($hours > 12 || $hours < 12) return false;
$add = 0;
break;
case 'PM':
if($hours == 12) return false;
$add = 43200;
break;
default:
$add = 0;
break;
}
return (int)(($hours * 3600) + ($minutes * 60) + $seconds + $add);
}
/**
* Returns the HH:mm:ss format of an integer. 1 being 00:00:01
* Examples: 5321 (01:28:41);
**/
function sec_to_time($sec) {
$diff = ($sec < 0 ? ($sec * -1) : $sec);
$h = floor($diff/3600);
$m = floor($diff/60) - ($h * 60);
$ts = $diff - ($h * 3600) - ($m * 60);
$hours = (strlen($h) == 1 ? '0'.$h : $h);
$minutes = (strlen($m) == 1 ? '0'.$m : $m);
$seconds = (strlen($ts) == 1 ? '0'.$ts : $ts);
return $hours.':'.$minutes.':'.$seconds;
}
/**
* Returns the number of hours, minutes and seconds between two times in a single day
* Examples: 12:00:00AM or 12:00AM
* Arguments are interchangeable
**/
function time_diff($time1,$time2) {
$newtime1 = $this->string_to_sec($time1);
$newtime2 = $this->string_to_sec($time2);
if(!$newtime1 || !$newtime2) return false;
return $this->sec_to_time($newtime1 - $newtime2);
}
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Date Time Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
|
|
|
|
You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
|
|
|
|
Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available. FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing. FREE! Go There Now!
|
|
|
|
This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |