dsdsds12222 2018-05-02 03:43
浏览 32
已采纳

如何通过插入另一个with minute:seconds:毫秒来获取插入时间的持续时间?

I am making a game mode in which I am trying to get the time a player has arrived at their destination after starting the mode and to do this I am using the insert of a date when it starts the mode it inserts a date and after reaching the your destination it registers another date and with both dates it calculates the time it took to get to the destination, with this I'm using date H:i:s (hours, minutes, seconds) but I need to take the time out and leave milliseconds after seconds example: i:s:u (minutes, seconds, milliseconds) but I'm not able to do this, I've tried it in several ways, basically everything works as follows:

1. I add in the player array a current date with hour, minutes, seconds;

$this->game[$player->getName()] = ["start" => strtotime('now')];

2. After the Player arrives at his destination he calculates the time of his trajectory creating another current date with already registered and using date and mktime to do the join and give a visual of time to the player;

$time = date('H:i:s', mktime(0, 0, str_replace("-", "", $this->game[$player->getName()]["start"] - strtotime('now'))));

3. Send the pretty message to the player about the time of his trajectory then time will be something like this: 01:45:23 (minute:seconds:milliseconds).

$player->sendMessage("You beat your time record by ".$time);

This is my method of doing, if you have another better method with the milli seconds added I accept the suggestion! Maybe there might be some errors in my code that I'm still not sure if they work correctly as the subtraction to calculate and join the current time with the previous one, tell me if it's right and if it is not correct correct me or do better. Thank you

  • 写回答

1条回答 默认 最新

  • duanhuang1967 2018-05-02 03:51
    关注

    Use microtime which returns the current Unix timestamp with microseconds

    $game = [];
    
    $game['start'] = microtime(true);
    // Do stuff
    sleep(3); // Without the sleep, start and end are the 'same'
    $game['end'] = microtime(true);
    
    $elapsedTime = ($game['end'] - $game['start']);
    
    $minutes = floor($elapsedTime / 60);
    $seconds = $elapsedTime % 60;
    $milliseconds = number_format($elapsedTime - floor($elapsedTime),3);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料