doumu5662 2010-08-27 10:36
浏览 38

使用php编写php Time()到mysql

I want to log the time that a user posted a message and display it in a Twitter like fashion.

I found a function that does this but it does not work with mysqls Timestamp type.

In the instructions it says that it uses the time()format to calculate it. How should I be writing the times to my Database in order for it to work???

This is the code:

function newTime($tm,$rcs = 0) {
    // http://snipplr.com/view/17338/
    $cur_tm = time(); $dif = $cur_tm-$tm;
    $pds = array('second','minute','hour','day','week','month','year','decade');
    $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
    for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
    $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
    if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
    return $x."ago";
}
  • 写回答

4条回答 默认 最新

  • doushen1026 2010-08-27 10:38
    关注

    I refuse to try and make sense out of that bloody mess :) Still laughing out loud.

    However, if you want to import timestamps into a mySQL DATETIME field - which I think is what you want - you can use FROM_UNIXTIME():

    INSERT INTO tablename (fieldname) VALUES (FROM_UNIXTIME('1234567890'));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起