PHP microtime to MySQL timestamp: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{exclaim}} Lose the accuracy after conversion. Microtime ex: 1426299057.8639 will lost the numeric after point (.8639) after saved to {{kbd | key = TIMESTAMP}} type ex: 2015-...") |
No edit summary |
||
| Line 24: | Line 24: | ||
//$timestamp: 2015-03-13 10:10:57 | //$timestamp: 2015-03-13 10:10:57 | ||
</pre> | </pre> | ||
== references == | |||
<references/> | |||
[[Category:PHP]] [[Category:Programming]] | [[Category:PHP]] [[Category:Programming]] | ||
[[Category:Web_Dev]] | [[Category:Web_Dev]] | ||
Revision as of 16:46, 17 April 2015
Lose the accuracy after conversion. Microtime ex: 1426299057.8639 will lost the numeric after point (.8639) after saved to TIMESTAMP type ex: 2015-03-14 10:10:57 (equals to 1426299057)
1. Check the MySQL server's current time zone: show variables like '%time_zone%';[1]. It's also the timezone which the data with TIMESTAMP type stored. example output:
Variable_name Value system_time_zone CST time_zone SYSTEM ## my SYSTEM timezone is Asia/Taipei
2. PHP codes (online demo)
//set the current timezone
date_default_timezone_set("Asia/Taipei");
$microtime = microtime(true);
//1426299057.8639
list($second, $numeric_after_point) = explode(".", $microtime);
//$second: 1426299057
//$numeric_after_point: 8639
$timestamp = date('Y-m-d H:i:s', $second);
//$timestamp: 2015-03-13 10:10:57