• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

PHP date/time function formating

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?

1 Solution
try gmdate http://www.php.net/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.
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.
djinnrutgerAuthor Commented:
Worked Perfect!

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now