Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vipin93's avatar
Level 13

How to encode and decode timstamp?

I'm using created_at as route parameter, but problem is that my url looks like

https://bae.dev/staff/section_student/1/2017-03-22%2000:51:47/attach

so i was thing before passing the url i should encode then returning i should decode so lke, I want like

 public function courses_for_students_sections()
    {         

      $courses = Course::where('user_id',Auth::user()->owner->id)->get();   

      return view('staff.attachment.courses_for_students_sections',compact('courses'));
    }

here the viewc "ourses_for_students_sections"

@foreach($courses as $course)
              <li><a href="/staff/section_student/{{$course->id}}/{{strtotime($course->created_at)}}/attach">{{ $course->name }}</a></li>
             @endforeach 

my action controller here for this url


 public function section_student_get($course = null, $created_at = null)
    {
       
        $students = Student::where('user_id',Auth::user()->owner->id)
                             ->whereHas('courses',function($q) use ($course, $created_at){
                              $q->where('id',$course)
                                ->where('created_at',$created_at); //here i comparing created_at to strtotime($course->created_at)
                             })->where('active',1)
                             ->with('courses','courses.sections')
                             ->doesntHave('studentacadmic')
                             ->get();  

      return view('staff.attachment.section_student',compact('students'));
    }

now when click the this link i got errors (as expected because i have not converted this in proper format)

Exception in Carbon.php line 291:
DateTime::__construct(): Failed to parse time string (1490124107) at position 7 (1): Unexpected character

so how can i decode strtotime($course->created_at) to this format "2017-04-02 21:10:25" so that i can compare

thanks

0 likes
3 replies
zachleigh's avatar

Why json encode the timestamp? What is your ideal url?

vipin93's avatar
Level 13

here my url look like

<li><a href="/staff/section_student/1/1490124107/attach">LKG</a></li>
vipin93's avatar
vipin93
OP
Best Answer
Level 13

i got solution

$date=Carbon::createFromTimeStamp($created_at);

Please or to participate in this conversation.