【问题标题】:Analog Clock - PHP with Javascript - date function模拟时钟 - 带有 Javascript 的 PHP - 日期函数
【发布时间】:2020-11-17 15:36:12
【问题描述】:

对于我的个人挑战,我正在尝试重新创建一个低保真房间。在房间里,我想显示一个带有当前时间的模拟时钟,完全用 PHP 编程。

我目前能够通过 if 语句“硬编码”它,但现在我想在正确的时间每分钟移动分钟部分。我在网上找不到它,所以我希望你能在这里帮助我!不知何故,我需要使它成为一个 for 循环,但我不知道如何遍历 css 部分。

不胜感激!

            <div class="clock">
                <?php
                    $m = date("i");
                    echo date("h:i:s");
                    
                    //Need to become a for/while loop
                    if ($m == 14){
                        echo '<div id="mins" style="transform: rotate(60deg);"</div>'; 
                    }
                ?>
                <div id="mins"></div>
            </div>
.clock{
    height: 150px;
    width: 150px;
    border-radius: 50%;
    background: white;
    position: absolute;
    left: 45%;
    top: 20%;
}

#mins{
    height: 60px;
    width: 5px;
    left: 50%;
    position: relative;
    transform: translateX(-50%);
    background: black;
    transform: rotate(0deg);
    transform-origin: bottom center;
}

【问题讨论】:

  • 我打赌你在使用 DateTime 时会更轻松地解决这个问题!!
  • 我认为您正在寻找 Javascript 解决方案。 PHP 无法每秒向浏览器返回一些内容。 PHP 只在客户端请求时返回一个服务器渲染页面。
  • 是的,使用 php 显示初始时间,然后创建一个 JavaScript 间隔,每分钟更新时钟光标
  • 谢谢大家...!如何将 PHP 时间与 javascript 连接起来?
  • 请用 Javascript 将问题从 PHP 编辑到 PHP。我将能够提供帮助。

标签: javascript php html date time


【解决方案1】:

我使用 Javascript 的模拟脚本解决方案。无需 PHP。

index.php

<div class="clock">
    <div id="hours"></div>
    <div id="minutes"></div>
    <div id="seconds"></div>
</div>

javascript

const updateInMS = 1000;

// Get the HTML elements from the page.
const clock = document.getElementsByClassName('clock')[0];
const htmHours = document.getElementById('hours');
const htmMinutes = document.getElementById('minutes');
const htmSeconds = document.getElementById('seconds');

// Start the timer
startTimer();

function startTimer() {
    // Trigger the tick function that loops the clock
    tick();
}

function tick() {
    setTimeout(function() {
        // Retrieve the date
        const now = new Date();
        const hours = now.getHours();
        const minutes = now.getMinutes();
        const seconds = now.getSeconds();

        const secondsInDegrees = (360 * seconds) / 60;
        const minutesInDegrees = (360 * minutes) / 60;
        const hoursInDegrees = (360 * hours) / 12;

        htmHours.style.transform = 'rotate(' + hoursInDegrees + 'deg)';
        htmMinutes.style.transform = 'rotate(' + minutesInDegrees + 'deg)';
        htmSeconds.style.transform = 'rotate(' + secondsInDegrees + 'deg)';

        tick();
    }, updateInMS);
}

css

.clock {
    height: 150px;
    width: 150px;
    border-radius: 50%;
    background: white;
    position: absolute;
    left: 45%;
    top: 20%;
}

.clock div {
    height: 60px;
    width: 5px;
    left: 50%;
    /* UPDATED THIS TO FIXED: */
    position: fixed;
    transform: translateX(-50%);
    background: black;
    transform: rotate(0deg);
    transform-origin: bottom center;
}

#hours {
    background: black;
}

#minutes {
    background: red;
}

#seconds {
    background: blue;
}

希望你喜欢我的解决方案。

【讨论】:

  • 旁注:由于您没有重新分配任何值,因此使用const 而不是let 会更有意义。
  • 当我尝试时,它的秒针和分针正确,但时针已关闭。你得到正确的结果了吗?
  • 我看到您已经将小时计算更正为 12 格。现在指针应该指向正确的时间。
  • 这是正确的@El_Vanja 谢谢你的提醒,感谢你。
猜你喜欢
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多