tags:

views:

19

answers:

1

Hi,

I've got a webapp (using php), my clients are java-based and are submitting dates to me using:

System.currentTimeMillis();

so my web app gets it via a post, so it's a string, like:

$submittedTime = "9734367890508";

I want to insert it into a mysql table field, I think either a DATETIME or TIMESTAMP field can store it.

What type of field should I use, and how do I get that submitted value into the mysql table such that I can do queries later on using all the mysql time functions?

Thanks

A: 

To use it with PHP, you'll have to divide it by 1000 to turn it into seconds. Once it's in seconds, you can format it with the date() command, like so:

<?php
    // Get timestamp from somewhere and assume it's $timestamp
    $timestamp = (int) ($timestamp / 1000);
    // MySQL takes year-month-day hour:minute:second format
    $mysql_datetime = date('%Y-%m-%d %H:%i:%s', $timestamp);
?>

Having said that, what's wrong with just using the time from PHP or MySQL themselves? It's be the same date call in PHP, but without the second argument; or just setting the value to NOW() in MySQL.

R. Bemrose
Oh the clients are submitting timestamps to me from java, so there's no chance to use any of the php time functions. In your example, the variable $timestamp will initially be a String value of the submitted timestamp. So does dividing by an integer automatically parse it from a string to an integer in php? Furthermore, will the automatic conversion be able to hold the size of the value (because it will be a long, not an int really). Thanks!
PHP should auto-convert the timestamp to a float (on 32-bit platforms; it'd be cast to an int on 64-bit platforms), which after it's divided by 1000, should fall into the int range prior to the cast to an int.
R. Bemrose
ok thanks I'll use this