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

earmsby's avatar

Store time interval?

What is the correct/best way to store a time interval (duration) in a model? I need to store the duration of pieces of music, so values would be something like: 00:15:25 or 01:30:00.

0 likes
7 replies
tykus's avatar
tykus
Best Answer
Level 104

An integer number of seconds (if that is the smallest time period you care about). You can convert H:i:s strings to seconds using a Mutator in the Model class if you are expecting to get the values in that format.

What are those values relating to; they do not obviously suggest interval IMHO.

2 likes
Sergiu17's avatar

Depends on what you need to do with the intervals, when used just for displaying them - then: 00:15:25 is completely fine.

If you want to sort, filter, probably the best is to save them in seconds.

For example, filter all the records that are from 2 to 5 minutes, sorting by length, and so one..

1 like
martinbean's avatar

@earmsby I work a lot with video. Durations can just be stored as a number of a seconds, so you can use either an int column if you want to store whole seconds, or a float column if storing sub-second precision is necessary.

1 like
earmsby's avatar

Thanks for the suggestions. Integer should be fine for my use. I do need to do math with the data, not just display, but I don't need precision below a whole second.

tykus's avatar

@earmsby given you need to do math on the value(s), having a number (i.e. seconds) makes most sense. Please mark the thread solved

earmsby's avatar

@tykus happy to mark it solved if I could figure out how. Where is the mechanism to mark it solved?

LaryAI's avatar
Level 58

To store a time interval or duration in a model, especially for something like the duration of pieces of music, you have a few options. The best approach depends on your specific requirements and how you plan to use this data. Here are a couple of common methods:

1. Store as an Integer (Seconds)

One of the most efficient ways to store a duration is to convert it into a total number of seconds and store it as an integer. This makes it easy to perform calculations and comparisons.

Pros:

  • Simple arithmetic operations.
  • Easy to sort and filter.

Cons:

  • Requires conversion to display in a human-readable format.

Implementation:

// Migration example
Schema::create('music_pieces', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->integer('duration_in_seconds'); // Store duration as an integer
    $table->timestamps();
});

// Model example
class MusicPiece extends Model
{
    protected $fillable = ['title', 'duration_in_seconds'];

    // Accessor to get duration in a human-readable format
    public function getDurationAttribute()
    {
        $hours = floor($this->duration_in_seconds / 3600);
        $minutes = floor(($this->duration_in_seconds % 3600) / 60);
        $seconds = $this->duration_in_seconds % 60;

        return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
    }
}

2. Store as a String (HH:MM:SS)

Alternatively, you can store the duration as a string in the HH:MM:SS format. This is more human-readable but can be less efficient for calculations.

Pros:

  • Human-readable format.
  • Easy to display without conversion.

Cons:

  • More complex to perform arithmetic operations.

Implementation:

// Migration example
Schema::create('music_pieces', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('duration'); // Store duration as a string
    $table->timestamps();
});

// Model example
class MusicPiece extends Model
{
    protected $fillable = ['title', 'duration'];

    // Method to convert duration to seconds if needed
    public function getDurationInSeconds()
    {
        list($hours, $minutes, $seconds) = explode(':', $this->duration);
        return $hours * 3600 + $minutes * 60 + $seconds;
    }
}

Conclusion

If you need to perform frequent calculations or comparisons, storing the duration as an integer (in seconds) is generally more efficient. If the primary concern is readability and you don't need to perform many calculations, storing it as a string might be more convenient. Choose the method that best fits your application's needs.

Please or to participate in this conversation.