Click to See Complete Forum and Search --> : time diff calulation call problem


tiddler
02-24-2004, 09:15 AM
Agggggg!
First of all I'm using php, mysql.
I've got a form that has a text entry and I'm trying to perform date difference calculation. The problem is I get a page error every time I run this line:


<input name="mag_time" type="text" tabindex="15" onChange="this.form.mag_timediff.value = days_between(<?php echo $trenddate->Fields('tddate'); ?>,<?php echo $tddateprevious->Fields('tddate'); ?> );" value="<?php echo htmlentities($param_tx2->Fields('mag_time')); ?>" size="8" maxlength="8">

The error comes up error expected ')' code 0 at char 56. I have two recordsets that return the date-time stamp of two records, these being trenddate and tddateprevious.

The function that I have in my javascript page is:

<!--

function days_between(date1, date2) {

// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24

// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()

// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)

// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)

}

//-->

If I equate the days_between call to days_between(); the error goes away. I'm not sure if the problem is in passing the data parameter in the php code or what

Can some one enlighten me with the error I've made.

Thanks

:confused:

tiddler
02-24-2004, 09:43 AM
Further information:

Here is the source code from browser:

<input name="mag_time" type="text" tabindex="15" onChange="this.form.mag_timediff.value = days_between(2004-02-19 04:02:46,2004-02-19 04:02:15);" value="4.0" size="8" maxlength="8">
Hours <br>

Also here is the field name and entry for mag_timediff:
<input name="mag_timediff" type="text" value="" size="8" maxlength="8" readonly="true">

:confused:

Pittimann
02-24-2004, 10:38 AM
Hi!

Please try this:

<?php
echo "<input name=\"mag_time\" type=\"text\" tabindex=\"15\" onChange=\"this.form.mag_timediff.value = days_between('".$trenddate->Fields("tddate")."','".$tddateprevious->Fields("tddate")."')\" value=\"".htmlentities($param_tx2->Fields("mag_time"))."\" size=\"8\" maxlength=\"8\">";
?>

Actually, I can't test it (no database :p). If it doesn't work:

You need single quotes around the arguments in the parsed code. It has to look like this:

<input name="mag_time" type="text" tabindex="15" onChange="this.form.mag_timediff.value = days_between('2004-02-19 04:02:46','2004-02-19 04:02:15');" value="4.0" size="8" maxlength="8">

If you call a js function with arguments which are not purely numeric, they have to be quoted...

Cheers - Pit

Edit - this should work:

<input name="mag_time" type="text" tabindex="15" onChange="this.form.mag_timediff.value = days_between('<?php echo $trenddate->Fields('tddate'); ?>','<?php echo $tddateprevious->Fields('tddate'); ?>');" value="<?php echo htmlentities($param_tx2->Fields('mag_time')); ?>" size="8" maxlength="8">

tiddler
02-24-2004, 03:29 PM
Thanks Pit

You were right I was not passing the information as a string and hence I neede the quotes as you suggested in the last set of code.

Now I'm dealing with a problem of the function.

I was under the impression since mysql datetime is stored as a date -time stamp that would be accepted by javascript as a valid date.

What I need to do is to get the valid yyyy-mm-dd hh:mm:ss string to be accepted as a javascript date value so that the function will work.

Todd

tiddler
02-25-2004, 10:41 AM
To any one who might read this thread:

The following is the js functions code that I ended up working with the mysql standard date field of YYYY-mm-dd hh:mm

<!--
function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (new Date(y, m, d))
return ((getMonth()==m) && (getDate()==d)) /* was y, m */ }
//-->

<!--
function ReadISO8601date(Q) { var T // adaptable for other layouts
//if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\d)$/.exec(Q)) == null) original
if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\d)/.exec(Q)) == null)
{ return -2 } // bad format
for (var j=1; j<=5; j+=2) T[j] = +T[j] // some use needs numbers
if (!ValidDate(T[1], T[3]-1, T[5])) { return -1 } // bad value
return [ T[1], T[3], T[5] ] }
//-->



<!--
function DiffDays(S1, S2) { // ISO date strings
var X = ReadISO8601date(S1) ; if (X<0) return 'Date 1 bad'
var Y = ReadISO8601date(S2) ; if (Y<0) return 'Date 2 bad'
var Dx = Date.UTC(X[0], X[1]-1, X[2])
var Dy = Date.UTC(Y[0], Y[1]-1, Y[2])
return (Dx-Dy)/864e5 }

//-->

hence the call in previous post html

The On Change portion became:
onChange="{this.form.mag_timediff.value = DiffDays('<?php echo $trenddate->Fields('tddate'); ?>','<?php echo $tddateprevious->Fields('tddate'); ?>');

Todd:)