doulu2011 2013-12-30 00:10
浏览 21
已采纳

从日期或毫秒开始的小时和分钟总和

Let`s say we have the time in milliseconds like this, and we want the get the sum of the 3 hours ( 02:00 + 03:00 + 01:30 = 06:30).

1388361603000 equals with Mon Dec 30 2013 02:00:03 GMT+0200
1388365202000 equals with Mon Dec 30 2013 03:00:02 GMT+0200
1388359845000 equals with Mon Dec 30 2013 01:30:45 GMT+0200

If i get the sum of those 3 i get :

4165086650000 equals with Tue Dec 27 2101 02:30:50 GMT+0200

If i get the sum divided by 1000 ( to get the seconds ) the result is this :

4165086650 equals with Wed Feb 18 1970 06:58:06 GMT+0200

Tried like this also and didnt worked :

$time1 = date("H:i", 1388361603000 / 1000);
$time2 = date("H:i", 1388365202000 / 1000);
$time3 = date("H:i", 1388359845000 / 1000);
$total_time = $time1 + $time2 + $time3;

The result was 6, if i remove the : between H and i the result will be 630 ( which still is not good ).

Something that i did and worked was this :

$hours1 = date("H", 1388361603000 / 1000);
$minutes1 = date("i", 1388361603000 / 1000);
$hours2 = date("H", 1388365202000 / 1000);
$minutes2 = date("i", 1388365202000 / 1000);
$hours3 = date("H", 1388359845000 / 1000);
$minutes3 = date("i", 1388359845000 / 1000);

$date = ($hours1 + $hours2 + $hours3) . ":" . ($minutes1 + $minutes2 + $minutes3);
$milliseconds = strtotime($date) * 1000;

echo $date = 6:30

echo $milliseconds = 1388377800000 which equals with Mon Dec 30 2013 06:30:00 GMT+0200

If i get hours bigger then 24 or minutes bigger then 60, i must convert it proper hours and minutes.

Is this the correct way of doing it or there is another simple way ?

  • 写回答

1条回答 默认 最新

  • doujinge9648 2013-12-30 00:18
    关注

    The most simple way I can imagine

    echo "Time is: ".date("Y-m-d H:i:s", strtotime("now +3 hours"));
    

    You can also feed strtotime() a UNIX timestamp as second param.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False