PHP date/time function formating

Posted on 2007-10-14
Last Modified: 2013-12-13
I am building a web site to track the amount of time spent on a project. basicly it has a form that when i click the submit button it records the time into a sql database as a start time, then the next click as a stop time. I am doing this in PHP and MYSQL. when it records the data, it is the number of seconds since jan 1 1970 useing the time() function.

I created a report to list the times and i use the format print date("g:i:s a", $items["start"]) and that give me the time it was recorded. and then at the end of the report i have it total up the records and i get the total seconds useing the function print date("h:i:s",$total). again it works but my hours are always set at 6. I am guessing that this is because of the time zone differance. i am in CST. is there a work around for this? or is there a better way to handle this?

Question by:djinnrutger
    LVL 48

    Expert Comment

    try gmdate

    Or write you own function to print out the seconds. Guess information will also be lost if the total exceeds 24h which will start from 0 again.
    LVL 2

    Accepted Solution

    Since you are calculating time and not a specific date in and of itself, I would make a function to break the # of seconds into seconds, hours, and minutes. What the date function is returning for you is the date that corresponds with the # of seconds after 0 in timestamp. You are just computing time...

    For carrying through reports, I would leave the time in seconds, and just format the output when needed for display...

    something like

    function timedisplay ($numseconds)  {
       $temptime = $numseconds;
       $hours = floor($temptime/3600);
       $temptime = $temptime%3600;
       $minutes = floor($temptime/60);
       $temptime = $temptime%60;
       $seconds = $temptime;
       echo( $hours . ":" . $minutes . ":" . $seconds);

    Use the above as a starter for where to go to calculate the time yourself, namely note the floor() and modulo (%) function.

    If you wanted to show days, you could using the same technique. But, I would guess if this is a billing application, you want everything in hours. However, if you are counting days, I think you can get that from this, too.

    Author Comment

    Worked Perfect!

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now