如果條件在幾小時、幾天內不起作用 (if condition not working for Hours,Days)


問題描述

如果條件在幾小時、幾天內不起作用 (if condition not working for Hours,Days)

見下面的代碼,如果條件不工作,從這段代碼中,我不知道如何寫這個的 if 條件

<?php                          
    $start = $row['t_started_on'];
    $due = date('Y‑m‑d H:i:s');
    $start_date = new DateTime($start);
    $end_date = new DateTime($due);
    $interval = $start_date‑>diff($end_date);
    echo "Result " . $interval‑>y . " years, " . $interval‑>m." months, ".$interval‑>d." days ";
 // upto here i got correct answer.
     if($interval‑>h <="24"){
  $result = $interval‑>h." Hours";// i want like this 10 Hours ago
 }else if ($interval‑>d <="30")){
   $result = $interval‑>d." Days";// i want like this 25 Days ago
 }else{
 $result = $interval‑>m." Months";// i want like this 10 Months ago
 }
    ?>


參考解法

方法 1:

Please try this

<?php                          
    $start = $row['t_started_on'];
    $due = date('Y‑m‑d H:i:s');
    $start_date = new DateTime($start);
    $end_date = new DateTime($due);
    $interval = $start_date‑>diff($end_date);
    echo "Result " . $interval‑>y . " years, " . $interval‑>m." months, ".$interval‑>d." days ";
 // upto here i got correct answer.
     if($interval‑>h <= 24){ // Removed quotes
        $result = $interval‑>h." Hours";// i want like this 10 Hours ago
     }
     else if ($interval‑>d <= 30)){ // Removed quotes
       $result = $interval‑>d." Days";// i want like this 25 Days ago
     }
     else{
       $result = $interval‑>m." Months";// i want like this 10 Months ago
     }
    ?>

方法 2:

    Hi,

One things that we have to check ,is t_started_on coming in Y‑m‑d H:i:s format ?.

Please try this.it should work.
<?php
$start = $row['t_started_on'];
$due = date('Y‑m‑d H:i:s');
$start_date = new DateTime($start);
$end_date = new DateTime($due);
$interval = $start_date‑>diff($end_date);
echo "Result " . $interval‑>y . " years, " . $interval‑>m." months, ".$interval‑>d." days ";
// upto here i got correct answer.
if($interval‑>h <= 24){
$result = $interval‑>h." Hours";// i want like this 10 Hours ago
}else if ($interval‑>d <= 30){
$result = $interval‑>d." Days";// i want like this 25 Days ago
}else{
$result = $interval‑>m." Months";// i want like this 10 Months ago
}
?>

Here's example.
$start = '2016‑03‑02 12:00:00 PM';
$due = date('Y‑m‑d H:i:s');
echo $due;
$start_date = new DateTime($start);
$end_date = new DateTime($due);
$interval = $start_date‑>diff($end_date);
echo "Result " . $interval‑>y . " years, " . $interval‑>m." months, ".$interval‑>d." days ";
// upto here i got correct answer.
if($interval‑>h <= 24 ){
$result = $interval‑>h." Hours";// i want like this 10 Hours ago
}else if ($interval‑>d <= 30 ){
$result = $interval‑>d." Days";// i want like this 25 Days ago
}else{
$result = $interval‑>m." Months";// i want like this 10 Months ago
}

i got this result : Result 0 years, 0 months, 30 days 21 Hours,it looks liek correct result.

Note : Result may be vary due to timezone.
</code></pre>

(by Kani Rdhi_mMahendra Sharma)

參考文件

  1. if condition not working for Hours,Days (CC BY‑SA 2.5/3.0/4.0)

#php-5.5 #PHP






相關問題

當作為參數傳遞時,PHP 如何解釋和評估函數和匿名函數? (How does PHP interpret and evaluate function and anonymous function when passed as an argument?)

使用 symfony 時 PHP 5.5 無法識別服務器 (PHP 5.5 won't recognise server when using symfony)

升級到 PHP5.5 時 Wordpress 崩潰 (Wordpress crashes when upgrading to PHP5.5)

從數據庫返回多個值 (return multiple values from database)

PHP:嵌套 foreach 循環的問題 (PHP: Issue with nested foreach loops)

如果條件在幾小時、幾天內不起作用 (if condition not working for Hours,Days)

Slim 3.3 輸出中缺少字符 (Slim 3.3 missing characters in output)

PHP排序對象值但保留原始相對順序 (PHP sort objects value but retain original relative order)

遇到錯誤無法加載請求的文件:helpers/form_validation_helper.php (An Error Was Encountered Unable to load the requested file: helpers/form_validation_helper.php)

如何從類的函數內部訪問全局變量 (How to access global variable from inside class's function)

如何修改輸出緩衝區? (How to modify output buffer?)

在同一台 Ubuntu 服務器上安裝和配置 PHP5 和 7 (Install and configure PHP5 and 7 on same Ubuntu server)







留言討論