Home  >  Q&A  >  body text

Simplify date representation and keep only the date part

<p>I am trying to get only the date part from the date representation. My database server is phpmyadmin and I retrieve data through laravel controller. I only need to get the date part, for example 2022-01-24 instead of 2022-01-24T18:30:00.000000Z from the created_at column. </p> <p>Image phpmyadmin. </p> <p>Image response</p> <p>My web.php path is: </p> <pre class="brush:php;toolbar:false;">Route::get('/fetch/curriculumVitaes', [App\Http\Controllers\Admin\CurriculumVitaesController::class, 'index']);< ;/pre> <p>I get data through laravel controller, my function is index():</p> <pre class="brush:php;toolbar:false;">public function index() { $curriculumVitaes = CurriculumVitaes::where('deleted', '=', '0')->get(); return $curriculumVitaes; }</pre> <p>I retrieve data in front-end vue.js: </p> <pre class="brush:php;toolbar:false;"><tr v-for="(curriculumVitae, index) in curriculumVitaes" :key="index"> <td class="text-center">{{ index 1 }}</td> <td class="text-center">{{ curriculumVitae.created_at }}</td> </tr></pre></p>
P粉834840856P粉834840856435 days ago508

reply all(1)I'll reply

  • P粉720716934

    P粉7207169342023-09-04 09:00:21

    I am not very familiar with Laravel and have not used Vue.js. But I think you can also solve this problem using core PHP. I've modified your code as shown below. When I retrieve a date from a MySQL database that I want to format, I use date('format', variable). In your case this would result in my code below. The format can be displayed in a variety of ways, including: 'm/d/y', 'd/m/y', 'y/d/m', etc. Where m represents the month, d represents the day, and of course y represents the year.

    As @Uwe pointed out, my suggestion is to embed the php code in the vue.js code. However, since I know you use both php and vue.js, I'm still wondering if my modified solution below will work. Or within that range.

    <tr v-for="(curriculumVitae, index) in curriculumVitaes" :key="index">
        <td class="text-center">{{ index + 1 }}</td>
        <td class="text-center">{{ <?php echo date("y/d/m", curriculumVitae.created_at); ?> }}</td>
    </tr>

    reply
    0
  • Cancelreply