timestamp conversion issue - +1 hour in certain instances?
Posted: Sun Oct 31, 2004 12:20 am
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
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;
}
if ($_POSTї'flag'] == "addEntry") {
$sd = $_POSTї'startDate'];
$ed = $_POSTї'endDate'];
$st = $_POSTї'startTime'];
$et = $_POSTї'endTime'];
$desc = $_POSTї'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>');
}