-1

我有以下整数位数...

$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 数据库中,我该如何实现。一些帮助?

4

3 回答 3

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

代码结果:http ://codepad.org/pmbIJdkT

于 2013-10-12T07:13:27.290 回答
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);
于 2013-10-12T08:02:16.530 回答
1

PHP

使用DateTime::createFromFormat方法:

$int = '20130923122843977';
$dt = DateTime::createFromFormat('YmdHisu', $int);
echo $dt->format('Y-m-d H:i:s');
# 2013-09-23 12:28:43

演示

MySQL

您可以使用STR_TO_DATE函数转换为日期时间格式:

SELECT STR_TO_DATE('20130923122843977', '%Y%m%d%H%i%s%x');
# 2013-09-23 12:28:43
于 2013-10-12T09:01:04.677 回答