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

如何通过插入另一个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

我正在制作一个游戏模式,我试图让玩家在开始后到达目的地的时间 模式和执行此操作我使用日期插入时,它启动它插入日期的模式,到达目的地后,它注册另一个日期和两个日期,它计算到达目的地所需的时间, 我正在使用日期H:i:s(小时,分钟,秒)但是我需要花时间在几秒钟之后离开毫秒例如:i:s:u(分钟,秒,毫秒)但我不是 能够做到这一点,我已经尝试过几种方式,基本上所有工作都如下: p>

1。 strong>我在播放器数组中添加了一个当前日期 小时,分钟,秒; p>

  $ this-> game [$ player-> getName()] = [“start”=>  strtotime('now')]; 
  code>  pre> 
 
 

2。 strong>在玩家到达目的地后,他计算出他的轨迹创建另一个的时间 当前日期已经注册并使用日期和mktime进行连接并向玩家显示时间; p>

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

3。 strong>向玩家发送关于他的轨迹时间的漂亮信息,然后时间会是这样的:01:45:23 (分:秒:毫秒)。 p>

  $ player-> sendMessage(“你用时间记录”。$ time); 
  code>   pre> 
 
 

这是我的做法,如果你有另一个更好的方法,加上毫秒我接受这个建议! 也许在我的代码中可能存在一些错误,我仍然不确定它们是否正常工作作为减法来计算并加入当前时间与前一个,告诉我它是否正确以及它是否正确纠正我或做 更好。 谢谢 p> div>

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);