Date Time Code
  Home arrow Date Time Code arrow class for some mysql imitated time functions
IBM Rational Software Development Conference
FaxWave - Free Trial.
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATE TIME CODE

class for some mysql imitated time functions
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2005-06-17

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    Build Forge Express demo: Enabling software delivery excellence for small and midsized businesses

    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!


    NEW! Develop with Java and PHP technology on AIX Version 5.3, Part 6: Building the Java business application

    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!


    NEW! Don't wait! Try the Rational Application Developer (RAD) v7.5 open beta code today

    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!


    NEW! Expand the editing capabilities of OpenOffice with XSLT

    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!


    NEW! IBM Rational Systems Development e-Kit

    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!


    NEW! Rational Talks to You: Scott Ambler on being agile in a global development environment

    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!


    NEW! Trial download: IBM Rational Tester for SOA Quality V7.0.1

    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!


    NEW! Try the IBM SOA Sandbox for Connectivity

    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!


    NEW! Web development with Eclipse Europa, Part 1: The Java EE for Eclipse

    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!


    NEW! Webcast: What is new in Viper 2 for developers?

    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!

    DATE TIME CODE ARTICLES

    - DaysInSpan.php
    - MySQLdateSpan.php
    - DateSpan.php
    - FutureDateFormEntry.php
    - Generate Time Option List for Select stateme...
    - Current Age Script (v2)
    - class for some mysql imitated time functions
    - Current Age Script, up to the last day
    - filemtime_remote
    - Page Generation Time Figure-Outer
    - convGMT v2
    - Benchmarker
    - Simple PHP Calendar
    - Display message according to hour of day
    - Display Date Function


     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway