doumubi6784 2017-06-02 13:58
浏览 44
已采纳

将javascript时间函数转换为php

I have an working javascript code that i use on an webpage to set text of an button. The text of the button show the time in 24 hour format with added 1 hour and 30 minutes rounded of to nearest 5 minutes. As i need to use the resulting time in an php code snippet on button click i need to convert my code to php syntax to get the same result.

As example if time now is 15:17 the result should be 16:50

<SCRIPT>
    var today = new Date();
    today.setHours(today.getHours() + 1);
    today.setMinutes(today.getMinutes() + 30);
    var coeff = 1000 * 60 * 5;
    var date = "09:07:00";  //or use any other date
    var rounded = new Date(Math.round(today.getTime() / coeff) * coeff)
    var tid_1hour30min_from_now = rounded.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
    var tid_hour = rounded.getHours();
    var tid_minutes = rounded.getMinutes();
    var ater = "Åter"
    var result1hour30minfromnow = ater +" "+ tid_1hour_from_now
</SCRIPT>

So i would like to do the same but in php.

Been trying with simular code to this:

$startTime = date("Y-m-d H:i:s");
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1,5 
hour',strtotime($startTime)));

$current_time = strtotime($cenvertedTime);

$frac = 900;
$r = $current_time % $frac;

$new_time = $current_time + ($frac-$r);
$new_date = date('Y-m-d g:i:s A', $new_time);

But this doesn't turn out well, i newer get the same time in both script above. How can i translate the javascript as close as possible to php?

/ Kristian

  • 写回答

4条回答 默认 最新

  • duanpo8329 2017-06-02 14:13
    关注

    It can be done with "normal" date() function also.

    $startTime = strtotime(date("Y-m-d H:i:s"))+90*60; // add 90 minutes times 60 seconds to UNIX time.
    $NewMinute = round(date("i", $startTime)/5)*5; // find new minute by rounding on "5"
    
    echo date("Y-m-d H:", $startTime) .str_pad($NewMinute, 2, "0", STR_PAD_LEFT);
    

    https://3v4l.org/fkGbQ

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分