Page 1 of 1

timestamp conversion issue - +1 hour in certain instances?

Posted: Sun Oct 31, 2004 12:20 am
by printelectric
Hello. I'm working on a basic service timesheet. Users will be able to log in, select from a list of active projects, and view/add timesheet entries, which are then stored in a mysql database.

So far, all of this is working:
User can enter a start date, a start time, an end date, and an end time. These values are posted to a PHP script, which is to pre-process for MySQL. I do this by converting the start date/time and the end date/time to timestamp values, and then getting the difference between the two to arrive at the minutes elapsed. My goal is to store the elapsed time in minutes as a MySQL float, with 2 decimal place - ie: 6.25.

PROBLEM:
My code is working for some times, but when the start time is PM, and the end time is AM of the next day, there is an extra hour being added. I'm echoing my time values after converting everything to 24 hour time, and they look fine. The problem seems to occur when using mktime(). I'm posting the essential part of my code, and the pages are online for anyone to play with. Any help greatly appreciated.

The page in question is online at:
http://www.printelectric.com/dev/pages/timesheets.php

The form I'm working with is the "Add New Entry" area.

Problem values:

start date: 10-30-04 start time: 11:00 pm
end date: 10-31-04 end time: 01:00 am

Code: Select all

function parseDateTime($date,$time) {
		/* arguments:
			$date is a string in the format mm-dd-yy
			$time is a string in the format hh:mm їam,pm]
		*/
		$mon = substr($date,0,2);
		$dd = substr($date,3,2);
		$yy = substr($date,-2);

		$hh = substr($time,0,2);
		$mm = substr($time,3,2);
		
		if ($hh == 12) {
			$hh = 0;
		}

		if (strpos($time,'pm')) {
			$hh += 12;
		}
				
		echo ('converted date and time is: '.$mon."/".$dd."/".$yy." ".$hh.":".$mm.'<br>');
		
		$u_ts =  mktime($hh,$mm,"",$mon,$dd,$yy);
		return $u_ts;
		
	&#125;
	
	if ($_POST&#1111;'flag'] == "addEntry") &#123;	
		$sd = $_POST&#1111;'startDate'];
		$ed = $_POST&#1111;'endDate'];
		
		$st = $_POST&#1111;'startTime'];
		$et = $_POST&#1111;'endTime'];
		
		$desc = $_POST&#1111;'description'];
				
		$unix_start = parseDateTime($sd,$st);
		$unix_end = parseDateTime($ed,$et);
		
		echo ($unix_start.'<br>');
		echo ($unix_end.'<br>');
		
		$diff = ($unix_end-$unix_start)/3600;
		echo('difference is ' .$diff.'<br>');
	&#125;

Posted: Sun Oct 31, 2004 1:07 am
by kettle_drum
Why not just fix the input so that it only allows 24 hour times to be selected and solve the problem that way. I.e. dont make users write in a date - select everything from drop down boxes - faster for users to do and means you dont have to mess about seeing if its am or pm.

Posted: Sun Oct 31, 2004 1:16 am
by printelectric
I don't think I want that large of a drop-down menu. I'd need 60 items for minutes. Also, I'm eventually going to add a timer function, so that the user can just hit a button to add a new timesheet entry, instead of entering manually. But if you look at my problem, the issue doesn't seem to be occurring in the conversion to 24 hour time. The issue is in the unix timestamp values being generated. If you go to the page and enter the values that I posted, you'll see what I mean. When I echo the 24 hour times after the conversion to 24 hour times - before the mktime() function is executed, everything is fine.

SOLVED - I'm sooooo stupid...

Posted: Sun Oct 31, 2004 1:55 am
by printelectric
I'm sorry everybody. The daylight savings time change is today in Dallas... I thought the changeover was tomorrow. But there really are 3 hours between 11:00 pm tonight and 1:00 am tomorrow. I was so focused on why the times weren't working it never occurred to me to try different dates.

I SINCERELY apologize to anyone who has wasted time on this issue. I'll try to be less of an idiot in the future.

Thanks -
Scott

P.S. - can't figure out how to mark this post as solved?

Re: SOLVED - I'm sooooo stupid...

Posted: Sun Oct 31, 2004 3:05 am
by Weirdan
printelectric wrote: P.S. - can't figure out how to mark this post as solved?
You can edit the subject of your first post. It will change the subject of entire thread.