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

Deekshith's avatar

Fetch strtotime data as time format with am and pm

Hi, I have a table with the columns start time and end time and it doesn't have any date. I have converted time to strtotime and Stored in a database. Now I want to know how to fetch time record in time format with am and pm using Db raw query or using any other way. Please guide me. I am not storing datetime here just storing time because this time data remains constant for all days. Ex : time: 10:00 AM converted to strtotime and storing in the database. I am using angularjs ng-repeat to display data in blade file.

0 likes
4 replies
JohnBraun's avatar

Since it seems you want to store a time value in your database (without a date), I came across this solution by @oeb on the forum: https://laracasts.com/discuss/channels/eloquent/database-storing-time-and-not-date.

To answer your specific question:

From what I understand is that you first parse a string like "10:00 AM" using strtotime(), which converts it into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

To (re)convert this timestamp into a readable time you would use the date() function in PHP.

// Look up the model (in this case a simple query)
$model = Model::first();

// fetch the timestamp from your database for the specific model
$unixTimestamp = $model->start_time;

// returns the time in 12h format
$date = date('H:i A', $unixTimestamp)
Deekshith's avatar

@johnbraun this can be done when we use laravel foreach to display data in blade file but i am using angularjs ng-repeat to display data in blade table.

realrandyallen's avatar
Level 44

@DEEKSHITH - One option is to use an accessor so you don't have to worry about using php or js to format the time in your view:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MyModel extends Model
{
    protected $appends = [
        'start_time_formatted'
    ];
        
    public function getStartTimeFormattedAttribute()
    {
        return date('H:i A', $this->start_time);
    }
}

https://laravel.com/docs/master/eloquent-mutators#defining-an-accessor

1 like

Please or to participate in this conversation.