簡體   English   中英

PHP和小時計算

[英]PHP and calculation with hours

我有一個邏輯問題。 我有這個:

$y=2014;
$m=05;
$d=22;
$d2=$d+1;

$sunset=17:52;
$sunrise=05:44;

我有一些在22和23天之間發生的事件,但是我只選擇在$ sunset和$ sunrise之間發生的事件。

我可以這樣說嗎:

if($event [$sunset<BETWEEN>$sunrise]){
//show something;
}

提前致謝。

使用DateTime()因為它們具有可比性,因此很容易:

$sunrise = new DateTime('2014-05-22 05:44');
$sunset  = new DateTime('2014-05-23 17:52');
$good    = new DateTime('2014-05-22 18:00');
$bad     = new DateTime('2014-05-23 18:00');
if ($good > $sunrise && $good < $sunset) {
    echo '$good is good';
}
if ($bad > $sunrise && $bad < $sunset) {
    echo '$bad is good';
}

演示版

您應該創建DateTime對象,然后就像計算時間差一樣簡單。

http://www.php.net/manual/en/datetime.diff.php

例:

<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>

暫無
暫無

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

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