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! |
This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process. FREE! Go There Now!
|
|
|
|
Set up a PHP Web interface for the Java(TM) business application using a database created in earlier in this series. The PHP Web interface collects information from users and sends the session data to the Java business application for processing and for a response. FREE! Go There Now!
|
|
|
|
Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
|
|
|
|
You might know that you can pull XML data into OpenOffice's spreadsheet program, Calc, but did you know that you can create a filter to make word-processing documents out of data stored as XML? This tutorial shows you how to use OpenOffice's import/export filters to open your XML data as though it's just a plain document. From there, users can edit the document much more naturally and then save it back to its native format. You can also use this feature to easily turn your documents into XML data. 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!
|
|
|
|
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. 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!
|
|
|
|
It's a good time to be a Web developer. You've never had more choices in terms of technologies. There are so many great open source Web servers, databases, programming languages, and development frameworks. No matter what combination of technologies you prefer to work with, there is an integrated development environment (IDE) that can increase your productivity: Eclipse. In this tutorial, Part 1 of a three-part "Web development with Eclipse Europa" series on how to use Eclipse for Web development with Java technology, PHP, and Ruby, we'll see how the latest release of Eclipse -- Europa -- can be used to rapidly develop Java Web applications. We'll use Java Platform, Enterprise Edition 5 (Java EE) for Eclipse to build a Web application for tracking and calculating baseball statistics. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |