如何将时间(秒)与名称联系起来


How to associate time (seconds) with a name

我试图从服务器获取时间(秒),然后将其关联到学生姓名:

    $info = getdate();
    $sec = $info['seconds'];
    $current_sec = $sec;
    echo $current_sec;
    echo " | ";
    if ($current_sec == '1') {echo 'John';}
    else if ($current_sec == '2') {echo 'Mary';}
    else if ($current_sec == '3') {echo 'Bill';}
    ~~~
    else if ($current_sec == '60') {echo 'Bob';}
    echo " | ";
    echo $current_sec;

然而,无论何时刷新页面,我似乎每次都得到相同的结果:

15 | John | 1

其中15为实际秒数。

你的if语句有一个打字错误:

 if ($current_sec = '1')

总是为真。试一试:

 if ($current_sec == '1')

因为比较运算符是==,而不是=