2

i am trying to display duration value, using database "column name" ( "date_of_join" ,"date_of_completion") value in view

like ( 1/1/2018 to 1/1/2019 ) this value show like ( 1 year )

( 1/1/2018 to 1/2/2018 ) value show like ( 1 month )

( 1/1/2018 to 10/2/2018 ) value show like (1 month 10 days )

how to do this in laravel how to show y=this in view

my view

    @if(isset($details))

    <table class="table table-striped">
           <thead>
                <tr>
                    <th>PHOTO</th>
                    <th>NAME</th>
                    <th>CERTIFICATE NO</th>
                    <th>COURSE</th>
                    <th>COURSE DURATION</th>
                    <th>DATE OF JOIN</th>
                    <th>CENTER NAME</th>
                </tr>
          </thead>
        <tbody>

            @foreach($details as $user)
              <td> 
                <td>{{$user->student_name}}</td>
                <td>{{$user->student_registration_id}}</td>
                <td>{{$user->student_course}}</td>
                <td>{{                     }}</td>
                <td>{{$user->date_of_join}}</td>
                <td>{{$user->center_name}}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
    @elseif(isset($message))
    <p style="color:red" align="center">{{$message}}</p>
    @endif
</div>

my route

Route::any( '/search', function(){
  $id = Input::get( 'id' );
  $certificate_issue_date = Input::get('certificate_issue_date');
  if($id != ""){

   $user = Student::where( 'student_registration_id', 'LIKE', '%' . $id . '%' );
   if (!empty($certificate_issue_date))
    {
       $user->where('certificate_issue_date','=',$certificate_issue_date)->where('student_certificate_approval', 'APPROVED');
    }
        $user = $user->get();

        if (count ( $user ) > 0)
     return view( 'verify_certificate' )->withDetails( $user )->withQuery($id)->withDateOfJoin($certificate_issue_date);
     }
  return view ( 'verify_certificate' )->withMessage ( "No Details found!" );
  } );
2
  • @TsaiKoga what is the good idea to put logic in different location Dec 4, 2019 at 9:51
  • Use Mutators, put the logic in model. View is just used to display datas.
    – TsaiKoga
    Dec 4, 2019 at 9:54

4 Answers 4

3

You can do it by Mutators;

And use Carbon to get the duration(for examples: diffInSeconds, diffInDays, diffForHumans, etc)

If you want the accurate format like that, you need to write it a method.

In your Student model:

Class Student extend Model
{
    ...
    public function getDurationAttribute() {
        return Student::diffToHumanInterval($this->date_of_join, $this->date_of_completion);
    }

    public static function diffToHumanInterval($start, $end) {
        $human = '';
        $start_date = \Carbon\Carbon::parse($start);
        $end_date = \Carbon\Carbon::parse($end);
        $years = $end_date->diffInYears($start_date);
        if ($years > 0) { 
            $human = $human.$years.' years ';
            $start_date = $start_date->addYears(years);
        }
        $months = $end_date->diffInMonths($start_date);
        if ($months > 0) {
            $human = $human.$months.' months ';
            $start_date = $start_date->addMonths($months);
        }
        $days = $end_date->diffInDays($start_date);
        if ($days > 0) {
            $human = $human.$days.' days ';
            $start_date = $start_date->addDays($days);
        }
        return $human;
    }
    ...
}

In your view:

<td>{{  $user->duration  }}</td>
5
  • @user12380208 it's not random numbers, is the duration seconds between these two date. I have changed to human language.
    – TsaiKoga
    Dec 4, 2019 at 8:04
  • its showing error Use of undefined constant days - assumed 'days' (this will throw an Error in a future version of PHP) Dec 4, 2019 at 9:49
  • @user12380208 I forget to add $ before days. I have updated...
    – TsaiKoga
    Dec 4, 2019 at 9:53
  • output result is "days" i what to show like ( 1 month or 1 year ) Dec 4, 2019 at 10:00
  • @user12380208 My mistakes, I have updated the answer .
    – TsaiKoga
    Dec 4, 2019 at 10:09
2

This is simple with PHP

 <?php 
  $datetime1 = new DateTime($date_of_join);
  $datetime2 = new DateTime($date_of_completion);
  $interval = $datetime1->diff($datetime2);
  echo $interval->format('%R%a days');
  $days = $interval->format('%a'); // only days you will get.
 ?>

You can then convert days in Months and Years as you want.

Edit :

You have to pass date_of_completion as well in below line

 return view( 'verify_certificate' )->withDetails( $user )->withQuery($id)->withDateOfJoin($certificate_issue_date);

In view

 @foreach($details as $user)
   @php
     $datetime1 = new DateTime($user->date_of_join);
     $datetime2 = new DateTime($user->date_of_completion);
     $interval = $datetime1->diff($datetime2);

     $days = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds'); 
   @endphp
     <td> 
     <td>{{$user->student_name}}</td>
     <td>{{$user->student_registration_id}}</td>
     <td>{{$user->student_course}}</td>
     <td>{{$days}}</td>
    <td>{{$user->date_of_join}}</td>
    <td>{{$user->center_name}}</td>
</tr>
 @endforeach
5
  • @Prashanth Deshmukh i was updated my ,view and route , please check and add guide me how to add your logic in my code thank you Dec 4, 2019 at 7:30
  • @user12380208 Hope updated answer will give some idea to you. Dec 4, 2019 at 7:40
  • @Prashanth Deshmukh its showing error "Undefined variable: date_of_completion" Dec 4, 2019 at 7:53
  • @user12380208 check now Dec 4, 2019 at 7:58
  • @Prashanth Deshmukh its showing error DateTime::__construct(): Failed to parse time string (13/12/2019) at position 0 (1): Unexpected character" Dec 4, 2019 at 8:28
1

Try this

<?php 
$datetime1 = new DateTime("2019-11-11");
$datetime2 = new DateTime("2020-11-12");
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $elapsed;
?>

refer link referlinkwithexample

 @foreach($details as $user)
   @php
     $datetime1 = new DateTime($user->date_of_join);
     $datetime2 = new DateTime($user->date_of_completion);
     $interval = $datetime1->diff($datetime2);
     $days = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds'); 
   @endphp
     <td> 
     <td>{{$user->student_name}}</td>
     <td>{{$user->student_registration_id}}</td>
     <td>{{$user->student_course}}</td>
     <td>{{$days}}</td>
    <td>{{$user->date_of_join}}</td>
    <td>{{$user->center_name}}</td>
</tr>
 @endforeach
5
  • i was updated my ,view and route , please check and add guide me how to add your logic in my code thank you Dec 4, 2019 at 7:30
  • i got error #message: "DateTime::__construct(): Failed to parse time string (13/12/2019) at position 0 (1): Unexpected character" #code: 0 Dec 4, 2019 at 8:46
  • in localhost its working when i shift the code on line its showing error Dec 4, 2019 at 8:53
  • your answer working perfect in local , but online its not working Dec 4, 2019 at 8:59
  • please give upvote this answer THANK YOU!!@user12380208 Dec 4, 2019 at 8:59
0

Use Carbon

$date->diffInDays($dateCondition); //result how many days left

diffInYears, diffInMonths

Not the answer you're looking for? Browse other questions tagged or ask your own question.