简体   繁体   English

Laravel Carbon-重新加载当前时间

[英]Laravel Carbon - Reloading Current Time

I could not find any document in relation to what I am trying to solve, hence I am asking it here. 我找不到与我要解决的问题有关的任何文档,因此在这里提出要求。

My controller pulls a Carbon Date using this: 我的控制器使用以下命令拉出Carbon Date:

$utctime = Carbon::now()->toTimeString();

That code allows me to display the UTC time on my page. 该代码使我可以在页面上显示UTC时间。 The problem comes in the fact that Carbon only loads the time once (on page load) and it stays at that time. 问题出在以下事实:Carbon仅加载一次时间(页面加载),而该时间保持在该时间。 What I want is to Carbon to display the real-time and not require the page to reload. 我想要的是Carbon显示实时信息,而不需要重新加载页面。

I am guessing it might not be possible the update the time, as Carbon is not front-end? 我猜这可能无法更新时间,因为Carbon不是前端? If so, how would you go about making the time update every second instead of it not updating at all? 如果是这样,您将如何使时间每秒更新一次而不是根本不更新呢? - Maybe there is something for the front-end that I can use? -也许我可以使用一些前端功能?

Adding a basic ticking clock in Javascript is fairly straightforward: 在Javascript中添加基本的滴答时钟非常简单:

<div id="time"></div>

<script type="text/javascript">
  function showTime() {
    var date = new Date(),
        utc = new Date(Date.UTC(
          date.getFullYear(),
          date.getMonth(),
          date.getDate(),
          date.getHours(),
          date.getMinutes(),
          date.getSeconds()
        ));

    document.getElementById('time').innerHTML = utc.toLocaleTimeString();
  }

  setInterval(showTime, 1000);
</script>

This would be entirely front-end based, and ensure there isn't a "jump" between the initially rendered server-time and the user's local time. 这将完全基于前端,并确保在最初呈现的服务器时间和用户的本地时间之间没有“跳”。

You can use moment.js, you can also define the timezone or display UTC time 您可以使用moment.js,也可以定义时区或显示UTC时间

moment.utc().format();

https://momentjs.com/docs/ https://momentjs.com/timezone/docs/ https://momentjs.com/docs/ https://momentjs.com/timezone/docs/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM