ラズベリーパイによって駆動されるデジタルサインとして使用するページがあります。このページには現在の天気だけでなく、日時も表示されます。date()を一度だけ呼び出す方法、さまざまな形式で複数回表示する方法
私はdate()
関数を3回別々に呼び出しています。 1つは天気アイコンのための昼夜かどうかを決定するためのもので、もう1つは時間を大きな数字で表示するもので、最後は現在の日付を表示するものです。
date()
を1つの変数に保存する方法はありますか?次に3つの異なる方法を使用しますか?あなたがdate()
に渡すことはあなたがそれを表示する形式であるため、
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
//header("Refresh: $sec; url=$page");
$bg = array(); // create an empty array
$directory = "images/"; //the directory all the images are in
$images = glob($directory . "*.jpg"); //grab all of the images out of the directory with .jpg extention
foreach($images as $image)
{
$bg[] = $image;//populate the empty array with an array of all images in the directory folder
}
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
$json_string="http://api.openweathermap.org/data/2.5/weather?lat=49.1985&lon=-113.302&appid=b29961db19171a5d4876c08caea9af0d&units=metric";
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
$now = date('U'); //get current time
$temp = round($obj['main']['temp']);
if($now > $obj['sys']['sunrise'] and $now < $obj['sys']['sunset']){
$suffix = '-d';
}else{
$suffix = '-n';
}
?>
<div id="todaysdatetime">
<div id="todaystime">
<span><?php echo(date("g:i A"));?></span>
</div>
<div id="todaysdate">
<span><?php echo(date("l\, F j<\s\up>S</\s\up>"));echo ' <i class="owf owf-', $obj['weather'][0]['id'].$suffix, '"></i> ', $temp, '°C'; ?></span>
</div>
</div>
3つの 'date()'呼び出しでは、実際にHTTPリクエストを行っているときに、コードの実行速度が遅くなることはありません。なぜコードが遅いのか推測しないで、ベンチマークしてください。 – CodeCaster
ベンチマークであるかどうかにかかわらず、いくつかの日付の呼び出しが目立つ量だけ何かを遅らせるというアイデアは、単に馬鹿げたものです。 – CBroe
@CBroeお役立ち情報私はちょうどそれが私のPCよりもπの方が遅くなるかもしれない理由について、この時点で推測しています。 – ShemSeger