PDA

View Full Version : Funky Javascript Date Function?



kuau
06-02-2008, 07:46 PM
2 days ago I moved a domain onto a host with php5. The old php4 code seemed to work OK except I am getting error messages on just one page. CORRECTION: the owner just told me it always gave error messages so it has nothing to do with php5 afterall (now he tells me), but it apparently works despite the messages. Seems to have something to do with the date functions in this javascript file:


function save_date () {
var Sel1_Date = document.database.Sel1_Day.value + "/" + document.database.Sel1_Month.value + "/" + document.database.Sel1_Year.value;
var Sel2_Date = document.database.Sel2_Day.value + "/" + document.database.Sel2_Month.value + "/" + document.database.Sel2_Year.value;
var now_date = Sel1_Date + "-" + Sel2_Date;
SetCookie ("Memo_Date",now_date);
}

Here are the error messages:


Warning: mktime() expects parameter 5 to be long, string given in /public_html/php/load_stats_data.php on line 42

Warning: mktime() expects parameter 5 to be long, string given in /public_html/php/load_stats_data.php on line 42

Warning: Division by zero in /public_html/php/load_stats_data.php on line 156

Warning: Division by zero in /public_html/php/load_stats_data.php on line 157


Here is line 42 in the php file it references:


$Show_Start_Date = $Sel1_Year."-".$Sel1_Month."-".$Sel1_Day;
$Show_End_Date = $Sel2_Year."-".$Sel2_Month."-".$Sel2_Day;
}


$Check_Days = (mktime(0,0,0,$Sel2_Month,$Sel2_Day,$Sel2_Year)) - (mktime(0,0,0,$Sel1_Month,$Sel1_Day,$Sel1_Year));
$Check_Days = $Check_Days / 86400;
$Check_Days = round($Check_Days,0);


and lines 156-7:


$Book_Begin_Days = round($Book_Begin_Days/$Counter,0);
$Book_Days = round($Book_Days/$Counter,0);
$Conversion = round($Conversion,0);

I think it is saying it is expecting a long integer but is getting a string, but other than that, I have no clue. I hope it makes sense to someone out there. Thanks! e :)

kuau
06-04-2008, 04:05 AM
I thought this would be a piece of cake for John et al. Nobody knows how to fix this?! I'm shocked. I tried setting Counter = 1 and it gets rid of the division by zero, but it skews the statistics. Here is what the errors look like. I notice there are as many errors as there are data lines.

http://www.carrentalhawaii.com/cp/statistic.html

Sorry to seem like such a dummy but I'm trying hard. Please help. It's beyond me, I'm afraid. Thanks, e

codeexploiter
06-04-2008, 06:22 AM
I don't know why you've shown the JS part which sets a cookie, you are getting PHP warnings so I think that part needs to be scrutinized.

1. About the 'mktime() expects parameter 5 to be long, string given' warning.
mktime function accepts all parameters as integer based ones, if you try to pass a non integer/number type value then it will show this warning. Make sure that you are passing all integer/number based values.

2. Division by zero warning
Most of the programming languages consider this operation an illegal one. If you can't reset the counter to 1 without creating problems try to avoid this division operation using some condition checks. As you know this kind of division operation is not going to give you anything so the operation need not be performed.

If you can post your PHP code completely that would be great so everyone can have a look at it and try to find the exact problem.

kuau
06-04-2008, 08:26 AM
Dear CE: Thanks so much for coming to my rescue. The message says parameter 5 on line 42 so I thought that meant $Sel2_Day and it is first mentioned in the javascript so that's why I showed that. Here's the code:


<? // Load Booking Stats Data for Statistics Report //

$table_dates = "dates";
$table_rates = "rates";
$table_check = "checks";
$table_booking = "booking";

// Dollar Sign

$P = CHR(36);

// Date Function

if(!$Sel1_Year) {
// Get Current Date

$da1 = getdate(time()-86400*7);
$month1 = $da1['mon'];
$day1 = $da1['mday'];
$year1 = $da1['year'];

// Make Date 2 Days in Advance

$da2 = getdate(time());
$month2 = $da2['mon'];
$day2 = $da2['mday'];
$year2 = $da2['year'];

$Show_Start_Date = $year1."-".$month1."-".$day1;
$Show_End_Date = $year2."-".$month2."-".$day2;
} else {
// Make correct date from form

//$New_Pick_Date = $Pick_Month."/".$Pick_Day."/".$Pick_Year;
//$New_Drop_Date = $Drop_Month."/".$Drop_Day."/".$Drop_Year;

$Show_Start_Date = $Sel1_Year."-".$Sel1_Month."-".$Sel1_Day;
$Show_End_Date = $Sel2_Year."-".$Sel2_Month."-".$Sel2_Day;
}


$Check_Days = (mktime(0,0,0,$Sel2_Month,$Sel2_Day,$Sel2_Year)) - (mktime(0,0,0,$Sel1_Month,$Sel1_Day,$Sel1_Year));
$Check_Days = $Check_Days / 86400;
$Check_Days = round($Check_Days,0);


// Open Main SQL Connection

include_once('cr-connectdb.php');

// Main Loop for Amount of Days

for($i=0; $i<=$Check_Days; $i++) {

// Reset values

$Counter = 0;
$Price_Checks = 0;
$Confirmed = 0;
$Book_Days = 0;
$Book_Begin_Days = 0;
$Book_Net_Price = 0;
$Commission = 0;
$Conversion = 0;
$Days_Ahead = 0;

// Make Date for Loop

$Check_Date = date("Y-m-d", mktime(0, 0, 0, $Sel1_Month, $Sel1_Day+$i, $Sel1_Year));


// Get Amount of Price Checks

$sql = "SELECT * FROM $table_check WHERE Ch_Date = '$Check_Date'";
$result2 = @mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
$Price_Checks = mysql_num_rows($result2);

$Sum_Price_Checks = $Sum_Price_Checks + $Price_Checks;


// Date in US

$S_Book_Date = explode("-", $Check_Date);
$S_Booking_Date = $S_Book_Date[1] . "/" . $S_Book_Date[2] . "/" . $S_Book_Date[0];


// Access Date (added "AND Book_Cancel = 0' on 6-28-07)

$sql = "SELECT * FROM $table_booking WHERE Book_Date = '$Check_Date' AND Book_Cancel = 0";
$result = @mysql_query($sql,$connection) or die("Couldn't execute $sql query.");

$S_Book_Cancel = $row['Book_Cancel'];
$S_Book_Confirm = $row['Book_Confirm'];

// Get through selected date booking entries

while ($row = mysql_fetch_array($result)) {

$Counter++;

// Confirmed

$S_Book_Confirm = $row['Book_Confirm'];
if($S_Book_Confirm != "Not confirmed") {
$Confirmed++;
}


// Days

$S_Book_Days = $row['Book_Days'];
$Book_Days = $Book_Days + $S_Book_Days;


// Days Ahead

$SQL_Book_Begin_Date = $row['Book_Begin_Date'];

$S_Book_Begin = explode("-", $SQL_Book_Begin_Date);

$Days_Ahead = (mktime(0,0,0,$S_Book_Begin[1],$S_Book_Begin[2],$S_Book_Begin[0])) - (mktime(0,0,0,$S_Book_Date[1],$S_Book_Date[2],$S_Book_Date[0]));
$Days_Ahead = $Days_Ahead / 86400;
$Days_Ahead = round($Days_Ahead,0);

$Book_Begin_Days = $Book_Begin_Days + $Days_Ahead;


// Net Revenue

if($S_Book_Confirm != "Not confirmed") {
$S_Book_Net_Price = $row['Book_Net_Price'];
$Book_Net_Price = $Book_Net_Price + $S_Book_Net_Price;
}


// Commission only when confirmed

if($S_Book_Confirm != "Not confirmed") {
$Commission = ($Book_Net_Price/100) * 15;
$Commission = round($Commission,0);
}


// Conversions

$Conversion = $Confirmed/($Counter/100);
}

// Make Average

$Sum_Counter = $Sum_Counter + $Counter;
$Sum_Confirmed = $Sum_Confirmed + $Confirmed;
$Sum_Book_Net_Price = $Sum_Book_Net_Price + $Book_Net_Price;
$Sum_Commission = $Sum_Commission + $Commission;

$Book_Begin_Days = round($Book_Begin_Days/$Counter,0);
$Book_Days = round($Book_Days/$Counter,0);
$Conversion = round($Conversion,0);

$Average_Conversion = $Average_Conversion + $Conversion;
$Average_Book_Days = $Average_Book_Days + $Book_Days;
$Average_Begin_Days = $Average_Begin_Days + $Book_Begin_Days;


// Output Data

if($Row == 1) {
echo "
<tr>
<td width='10%' bgcolor='#F3F3F3'><font size='2' face='Arial'>$S_Booking_Date</font> </td>
<td width='12%' bgcolor='#F3F3F3'><font size='2' face='Arial'>$Price_Checks</font> </td>
<td width='10%' bgcolor='#F3F3F3'><font size='2' face='Arial'>$Counter</font> </td>
<td width='12%' bgcolor='#F3F3F3'><font size='2' face='Arial'>$Confirmed</font> </td>
<td width='9%' bgcolor='#F3F3F3' nowrap><font size='2' face='Arial'>$Book_Days</font> </td>
<td width='11%' bgcolor='#F3F3F3' nowrap><font size='2' face='Arial'>$Book_Begin_Days</font> </td>
<td width='11%' bgcolor='#F3F3F3'><font size='2' face='Arial'>$$Book_Net_Price</font> </td>
<td width='11%' bgcolor='#F3F3F3'><font size='2' face='Arial'>$$Commission</font> </td>
<td width='9%' bgcolor='#F3F3F3'><font size='2' face='Arial'>$Conversion%</font> </td>
</tr>
";
$Row = 0;
} else {
echo "
<tr>
<td width='10%' bgcolor='#E6E6E6'><font size='2' face='Arial'>$S_Booking_Date</font> </td>
<td width='11%' bgcolor='#E6E6E6'><font size='2' face='Arial'>$Price_Checks</font> </td>
<td width='10%' bgcolor='#E6E6E6'><font size='2' face='Arial'>$Counter</font> </td>
<td width='5%' bgcolor='#E6E6E6'><font size='2' face='Arial'>$Confirmed</font> </td>
<td width='13%' bgcolor='#E6E6E6' nowrap><font size='2' face='Arial'>$Book_Days</font> </td>
<td width='13%' bgcolor='#E6E6E6' nowrap><font size='2' face='Arial'>$Book_Begin_Days</font> </td>
<td width='11%' bgcolor='#E6E6E6'><font size='2' face='Arial'>$$Book_Net_Price</font> </td>
<td width='9%' bgcolor='#E6E6E6'><font size='2' face='Arial'>$$Commission</font> </td>
<td width='9%' bgcolor='#E6E6E6'><font size='2' face='Arial'>$Conversion%</font> </td>
</tr>
";
$Row = 1;
}

}

// Average Conversion

$Average_Conversion = round($Average_Conversion/($Check_Days+1),0);
$Average_Book_Days = round($Average_Book_Days/($Check_Days+1),0);
$Average_Begin_Days = round($Average_Begin_Days/($Check_Days+1),0);


// Show Sum for Net & Full Price

echo "
<tr>
<td width='10%' bgcolor='#E6E6E6'><font size='2' face='Arial'><b>Sum</b></font> </td>
<td width='11%' bgcolor='#E6E6E6'><font size='2' face='Arial'><b>$Sum_Price_Checks</b></font> </td>
<td width='10%' bgcolor='#E6E6E6'><font size='2' face='Arial'><b>$Sum_Counter</b></font> </td>
<td width='5%' bgcolor='#E6E6E6'><font size='2' face='Arial'><b>$Sum_Confirmed</b></font> </td>
<td width='13%' bgcolor='#E6E6E6' nowrap><font size='2' face='Arial'><b>$Average_Book_Days</b></font> </td>
<td width='13%' bgcolor='#E6E6E6' nowrap><font size='2' face='Arial'><b>$Average_Begin_Days</b></font> </td>
<td width='11%' bgcolor='#E6E6E6'><font size='2' face='Arial'><b>$$Sum_Book_Net_Price</b></font> </td>
<td width='9%' bgcolor='#E6E6E6'><font size='2' face='Arial'><b>$$Sum_Commission</b></font> </td>
<td width='9%' bgcolor='#E6E6E6'><font size='2' face='Arial'><b>$Average_Conversion%</b></font> </td>
</tr>
";

// End Program

exit;

// Make cents always two digits

function form_sum($number) {
$Dollar = strtok($number,".");
$Cent = substr(strrchr($number,"."),1);

if (strlen($Cent) == 0) {
$Cent = "00";
}

if (strlen($Cent) == 1) {
$Cent = $Cent."0";
}

$number = $Dollar.".".$Cent;
return $number;
}
?>

codeexploiter
06-04-2008, 09:20 AM
It seems that the following section has some logical issues in it.


if(!$Sel1_Year) {
// Get Current Date

$da1 = getdate(time()-86400*7);
$month1 = $da1['mon'];
$day1 = $da1['mday'];
$year1 = $da1['year'];

// Make Date 2 Days in Advance

$da2 = getdate(time());
$month2 = $da2['mon'];
$day2 = $da2['mday'];
$year2 = $da2['year'];

$Show_Start_Date = $year1."-".$month1."-".$day1;
$Show_End_Date = $year2."-".$month2."-".$day2;
} else {
// Make correct date from form

//$New_Pick_Date = $Pick_Month."/".$Pick_Day."/".$Pick_Year;
//$New_Drop_Date = $Drop_Month."/".$Drop_Day."/".$Drop_Year;

$Show_Start_Date = $Sel1_Year."-".$Sel1_Month."-".$Sel1_Day;
$Show_End_Date = $Sel2_Year."-".$Sel2_Month."-".$Sel2_Day;
}


$Check_Days = (mktime(0,0,0,$Sel2_Month,$Sel2_Day,$Sel2_Year)) - (mktime(0,0,0,$Sel1_Month,$Sel1_Day,$Sel1_Year));

The if condition checks whether the year part has any meaningful value in $Sel1_Year variable, if not you are computing it otherwise you go to the else part. If you go to else part directly, means the year part has correct value, then the next statement seems to be logically correct. But if you go to the true part then the next statement after the if construct seems to be incorrect. If you look in the true part you've never assigned any values to $Sel2_Month,$Sel2_Day,$Sel2_Year,$Sel1_Month,$Sel1_Day,$Sel1_Year variables where we already knew that the $Sel1_Year variable doesn't contain any meaningful value and the mktime operation is performed directly on these variables which may not have correct values as a result it throws some warning/errors.

Like that in your code I am unable to find the first reference of $Sel2_Month,$Sel2_Day,$Sel2_Year,$Sel1_Month,$Sel1_Day,$Sel1_Year variables. As a result unable to find whether those variables contains any non integer/number values like string, null, boolean, etc.

Please note that the variable you have in your client-side script is not accessible from PHP unless they've been passed to the server-side exclusively.

kuau
06-07-2008, 06:31 PM
Dear CE:

Thank you so much for explaining what the code was doing. Because of that I was able to track down a place in another file where it said this:


var id = GetCookie('Memo_Date');

var date = new Date();
var d = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
/* var year = (yy < 1000) ? yy + 1900 : yy; */
var year = (yy < 1000) ? yy + 2000 : yy;

var Sel1_Date = month + "/" + day + "/" + year;
var Sel2_Date = Sel1_Date;
var MDate = Sel1_Date + "-" + Sel2_Date;


I changed the 1900 to 2000 and suddenly all the messages went away. I am not sure why -- I figure using 1900 must have caused the date to become a negative number or something. Anyway, it now works perfectly, so I can move onto other things.

Thank you so much for your help. I go crazy when things don't work properly. I so appreciate this forum -- otherwise I would be alone in the night banging my head against the wall. Thanks a million! e :)