Using java System.currentTimeMillis() with a php script and mysql table?
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开发者_StackOverflow中文版 on using all the mysql time functions?
Thanks
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.
精彩评论