I'm a little green with unix timestamps and mktime(), so I need some assistance. I'm attempting to construct a time calculator. I'm at a stage where I'm trying to convert the time and date provided (on previous form) to unix timestamp. Here's my code:

$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$hour1i = $_POST['h1i'];
$minutes1i = $_POST['m1i'];
$hour1o = $_POST['h1o'];
$minutes1o = $_POST['m1o'];


$n1 = $hour1i.", ".$minutes1i.", 00, ".$month.", ".$day.", ".$year;
$o1 = $hour1o.", ".$minutes1o.", 00, ".$month.", ".$day.", ".$year;
echo $n1."<br/>";
echo $o1."<br/>";

$n1u = mktime($n1);
$o1u = mktime($o1);

echo $n1u."<br/>";
echo $o1u."<br/>";

$total = $o1u - $n1u;

echo "You worked ".$total." seconds today!";

It returns the following results when the hours given are 8:00 and 11:30:

08, 00, 00, 12, 21, 2007
11, 30, 00, 12, 21, 2007
1198252590
1198263390
You worked 10800 seconds today!

Something is going wrong in the mktime() conversion to unix timestamp. The number of seconds between 8:00 and 11:30 should be 12600, not 10800. Now if I change the times to 8:00 and 11:00, it displays 10800, which is correct. And if I go to 8:00 and 12:00, it displays 14400, which is also correct. But any other combination of times which do not end in :00 returns incorrect values. Any suggestions?

    You will need to supply each argument to mktime() separately. Concatenating them into a single string variable means that the string is converted into an integer value in the function all, so it is as if all you entered is a value for the hour (therefore the rest of the values will be derived from the current date/time).

      Write a Reply...