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! |
Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. 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!
|
|
|
|
Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
|
|
|
|
IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
|
|
|
|
Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications. FREE! Go There Now!
|
|
|
|
You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |