簡體   English   中英

PHP不會在while循環中更新當前時間

[英]PHP is not updating the current time inside while loop

我正在嘗試獲取當前時間,並使用PHP CLI腳本在控制台中顯示當前時間。 問題是,無法在無限運行但每秒休眠的while循環內獲取當前時間。 無論我在程序啟動時獲得的時間如何,都會一直堅持到程序結束。

<?php

date_default_timezone_set("Asia/Kolkata");

while(1) {
    $ts = new DateTime();
    $ts->getTimestamp();
    $now_ts = $ts->format("H:m:i");
    echo "\n{$now_ts}";

    sleep(1);
}

無法理解這里發生了什么。 請幫忙。

$ts->format("H:m:i");

應該

$ts->format("H:i:s");

因為i代表分鍾。

m是一個月的數字表示,前導零

http://php.net/manual/zh/function.date.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM