ホームページ >バックエンド開発 >PHPチュートリアル >PHPは秒を時間(年、日、時、分、秒)に変換します
$t=1637544;
$d=Sec2Time($t);
$d は 0 年、18 日、22 時間、52 分 24 秒です
//秒を時間に変換します (年、日) 、時、分、秒)
<span>function</span> Sec2Time(<span>$time</span><span>){ </span><span>if</span>(<span>is_numeric</span>(<span>$time</span><span>)){ </span><span>$value</span> = <span>array</span><span>( </span>"years" => 0, "days" => 0, "hours" => 0, "minutes" => 0, "seconds" => 0,<span> ); </span><span>if</span>(<span>$time</span> >= 31556926<span>){ </span><span>$value</span>["years"] = <span>floor</span>(<span>$time</span>/31556926<span>); </span><span>$time</span> = (<span>$time</span>%31556926<span>); } </span><span>if</span>(<span>$time</span> >= 86400<span>){ </span><span>$value</span>["days"] = <span>floor</span>(<span>$time</span>/86400<span>); </span><span>$time</span> = (<span>$time</span>%86400<span>); } </span><span>if</span>(<span>$time</span> >= 3600<span>){ </span><span>$value</span>["hours"] = <span>floor</span>(<span>$time</span>/3600<span>); </span><span>$time</span> = (<span>$time</span>%3600<span>); } </span><span>if</span>(<span>$time</span> >= 60<span>){ </span><span>$value</span>["minutes"] = <span>floor</span>(<span>$time</span>/60<span>); </span><span>$time</span> = (<span>$time</span>%60<span>); } </span><span>$value</span>["seconds"] = <span>floor</span>(<span>$time</span><span>); </span><span>//</span><span>return (array) $value;</span><span>$t</span>=<span>$value</span>["years"] ."年". <span>$value</span>["days"] ."天"." ". <span>$value</span>["hours"] ."小时". <span>$value</span>["minutes"] ."分".<span>$value</span>["seconds"]."秒"<span>; </span><span>Return</span><span>$t</span><span>; }</span><span>else</span><span>{ </span><span>return</span> (bool) <span>FALSE</span><span>; } }</span>
以上、秒を時間(年、日、時、分、秒)に変換するPHPを内容も含めて紹介しましたので、PHPチュートリアルに興味のある友人の参考になれば幸いです。