我有以下数字整数...
$int = 20130923122843977;
// This year yyyy - 2013
// This Month mm - 09
// This Date dd - 23
// This Hour hh - 12
// Minutes mn - 28
// This Seconds se - 43
// Millisecond ms - 43
我试图使用substr。
来获取它$yy = substr($time, 0, 4); //return year (YYYY) -
但令人困惑的是当我来到秒和毫秒时,因为我应该将它插入到mysql数据库中,我该如何实现。 一些帮助?
答案 0 :(得分:2)
好的,如果您只有这种格式时间字符串:
<?php
$int = 20130923122843977;
$Y = substr($int, 0, 4);
$m = substr($int, 4, 2);
$d = substr($int, 6, 2);
$H = substr($int, 8, 2);
$i = substr($int, 10, 2);
$s = substr($int, 12, 2);
$ms = substr($int, 14);
$time = mktime($H, $i, $s, $m, $d, $Y);
echo date('Y-m-d H:i:s', $time); //2013-09-23 12:28:43
答案 1 :(得分:1)
这是你需要的功能,你的$ int都搞砸了,但是我的功能仍然可以克服它,你仍然可以输入整数或字符串=]
毫秒未使用,但仍可供参考
$int = 2013092312284343;
function weird_date_to_timestamp ($time)
{
// convert to string (if you cast you get exponential notation)
$time = number_format($time, 0,'','');
$time = str_split($time, 2);
$a['year'] = $time[0] . $time[1];
$a['month'] = $time[2];
$a['day'] = $time[3];
$a['hour'] = $time[4];
$a['min'] = $time[5];
$a['sec'] = $time[6];
$a['msec'] = $time[7];
date_default_timezone_set('UTC');
return strtotime("{$a['year']}-{$a['month']}-{$a['day']} {$a['hour']}:{$a['min']}:{$a['sec']}");
}
$t_stamp = weird_date_to_timestamp($int);
// you can give this out as input to mysql
print date("Y-m-d H:i:s", $t_stamp);
答案 2 :(得分:1)
使用DateTime::createFromFormat
方法:
$int = '20130923122843977';
$dt = DateTime::createFromFormat('YmdHisu', $int);
echo $dt->format('Y-m-d H:i:s');
# 2013-09-23 12:28:43
您可以使用STR_TO_DATE
功能转换为日期时间格式:
SELECT STR_TO_DATE('20130923122843977', '%Y%m%d%H%i%s%x');
# 2013-09-23 12:28:43