Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From Javascript to PHP to Mysql - a time variable. 2

Status
Not open for further replies.

ruffy

Programmer
Oct 1, 2003
72
0
0
US
I want to trap the TIME an event started, to insert it into a MySql table.

In my javascript, I wrote:
start = new Date( )
starttime = start.getTime( );

When I do that my alert shows a 13 digit number. (OK, I guess).
But now I need to INSERT that time into a MySql table.
So I send it off, as a "get parameter", using:
url = "ajaxUpdate.php?began=" + starttime;

Having received this parameter, I do not know how to
transform it, using PHP, so a mysql_query can use to reflect
the time gotten from the javascript function.
 
getTime() reports time in milliseconds since the start of the unix epoch. php does not like the milliseconds.

so to convert to a mysql type timestamp

Code:
$mysql = convertJSTimeToMySqlString('     ');//insert value here...

function convertJSTimeToMySqlString($jsTime){
 $unixTime = floor($jsTime/1000); //divide by 1000 and get int value
 return date("Y-m-d H:i:s O", $unixTime);
}
 

This ajax thing doesn't allow me to echo my results;
Apparently it's as if the echo would be interpreted as the "return" of this php file,
though I'm not sure why. Anyways,
because I could not call your function, I
1) coded it inline, and
2) put the value into a mysql table.

and it came up wrong! With a zero value.
The inline code is this:
$dialstart = (int) $_GET["dialstart"]; // get start time (shows up correctly)
$unixTime = floor($dialstart/1000); //divide by 1000 and get int value
$starttime = date("Y-m-d H:i:s O", $unixTime); // this yields zero
mysql_query("INSERT INTO holdvalue (holdvalue) VALUES (\"$starttime\")");

Do you see something wrong here?
 
try missing off the O from the date format. it may be that mysql does not like fully formatted dates
Code:
$starttime     = date("Y-m-d H:i:s", $unixTime);  // this yields zero

 
My TIME value captured in javascript is still an issue.
I see I opened a bag of worms, but I'll try to keep this time issue simple.

My Mysql table has 4 fields:
1) A char field 15 long.
2) An int field 15 long.
3) A Time field, and
4) A Date field.

My javascript gets a time in milliseconds using the getTime() method.
This is a value of 13 digits!

My php file receives this value as follows:
$phpdate = $_GET["dialstart"];


I deliver it to the Mysql table as follows, to see what happens
mysql_query("INSERT INTO holdtbl (holdno, holdch, holdtime, holddate) VALUES (\"$phpdate\",\"$phpdate\",\"$phpdate\",\"$phpdate\")", $dbh);


The character field shows the number correctly.
The integer field shows a number of 10 digits only!
Que 1 - why?

If I run this code:
$unixTime = floor($phpdate/1000);
$mysqldate = date( 'Y-m-d H:i:s', $unixTime);

and then
mysql_query("INSERT INTO holdtbl (holdno, holdch, holdtime, holddate) VALUES (\"$mysqldate\",\"$mysqldate\",\"$mysqldate\",\"$mysqldate\")", $dbh);


I get the very same results.
Que 2 - why?

Que 3 - How do I simply(?) get a time stamp into that Mysql time field,
and a date stamp into that date field?
 
ruffy said:
My TIME value captured in javascript is still an issue.
I see I opened a bag of worms, but I'll try to keep this time issue simple.

My Mysql table has 4 fields:
1) A char field 15 long.
2) An int field 15 long.
3) A Time field, and
4) A Date field.

My javascript gets a time in milliseconds using the getTime() method.
This is a value of 13 digits!

My php file receives this value as follows:
$phpdate = $_GET["dialstart"];

I deliver it to the Mysql table as follows, to see what happens
mysql_query("INSERT INTO holdtbl (holdno, holdch, holdtime, holddate) VALUES (\"$phpdate\",\"$phpdate\",\"$phpdate\",\"$phpdate\")", $dbh);

The character field shows the number correctly.
The integer field shows a number of 10 digits only!
Que 1 - why?
Basically because an int field can only ever be 10 characters long. You can't actually set a length for the int field it is what it is. Look here more more info on fields:

ruffy said:
If I run this code:
$unixTime = floor($phpdate/1000);
$mysqldate = date( 'Y-m-d H:i:s', $unixTime);
and then
mysql_query("INSERT INTO holdtbl (holdno, holdch, holdtime, holddate) VALUES (\"$mysqldate\",\"$mysqldate\",\"$mysqldate\",\"$mysqldate\")", $dbh);

I get the very same results.
Que 2 - why?

Que 3 - How do I simply(?) get a time stamp into that Mysql time field,
and a date stamp into that date field?

the date fields and time fields expect 2 different things. you need to pass what they expect:

Code:
$unixTime = floor($phpdate/1000); 
[blue]$mysqldate = date( 'Y-m-d' , $unixTime);[/blue]
[red]$mysqltime=date('H:i:s, $unixTime);[/red]
and then
mysql_query("INSERT INTO holdtbl (holdno, holdch, holdtime, holddate) VALUES (\"$mysqldate\",\"$mysqldate\",\"[blue]$mysqldate[/blue]\",\"[red]$mysqltime[/red]\")", $dbh);





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top