如果条件不适用于小时,天

时间:2016-04-02 06:34:13

标签: php php-5.5

请参阅下面的代码,如果条件不起作用,请从此代码中获取,我不知道如何为此条件设置

<?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
 }
    ?>

2 个答案:

答案 0 :(得分:0)

请试试这个

<?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
     }
    ?>

答案 1 :(得分:0)

    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.