Display date format as DD MM YYYY in active record Codeigniter

Display date format as DD MM YYYY in active record Codeigniter

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Formatting Dates in CodeIgniter Active Record: Moving from ISO to DD MM YYYY

When dealing with database timestamps, the default output from MySQL is almost always in YYYY-MM-DD format. While this standard ISO 8601 format is excellent for sorting and machine processing, it often fails when presenting data directly to users who expect a specific calendar layout like DD-MM-YYYY.

In CodeIgniter, you might find yourself trying to force the database column itself to change its output format using MySQL's DATE_FORMAT function. A common attempt looks like this in your view:

<?php echo $row->DATE_FORMAT(start_date, '%d/%m/%Y'); ?>

This approach won't work because $row contains a plain PHP object or array resulting from the query execution; it does not retain knowledge of SQL functions. You cannot call MySQL functions directly on the result set in the view layer unless you explicitly inject them into the SELECT clause or handle the data transformation differently.

The most robust solution involves handling the formatting either within your Model or strictly within your View, depending on your architectural preference for separation of concerns.

Formatting Within the View

The simplest fix requires no changes to your model logic. You can utilize PHP's built-in Carbon library (if available) or native date functions directly in the view file where you are rendering the data.

If you are using CodeIgniter 3.x or have Carbon installed, which is common in Laravel-based projects or when using the phpcarbon/laravel-carbon package logic ported over, you can instantiate a new Carbon instance from the raw date string and reformat it:

use Carbon\Carbon;

// Assuming $row->start_date is a string like '2016-01-07'
echo Carbon::parse($row->start_date)->format('d/m/Y');

This outputs 07/01/2016. If you strictly need spaces between the numbers instead of slashes, adjust the format string to 'd m Y'. This method is flexible because it keeps your database schema unchanged and allows you to tweak the display format without touching the backend.

Formatting in the Model

If you prefer to keep your views clean and avoid date manipulation logic scattered across multiple templates, you should handle the transformation inside the Model's query or retrieval function.

Modify your get_bank method to cast the result into a Carbon instance immediately after fetching it:

class Profiles_model extends CI_Model {

    function get_bank()
    {
        $this->db->where('mem_id', $this->session->userdata("id"));
        $query = $this->db->get('bank');
        
        return $query->result();
    }

}

While this doesn't change the object returned, you can create a custom accessor or modify the specific row if needed. However, a cleaner approach in the model is to fetch the raw data and then format it before returning, though this requires knowing exactly which columns need formatting. A better practice for Active Record queries is to let the view handle presentation logic unless the data is being serialized for JSON responses.

The Trade-off: Data Integrity vs. Presentation

It is crucial to remember that date formatting belongs in the presentation layer (View), not the data layer (Model or Database). Changing how a date is displayed does not alter the underlying value stored in your database. If you format dates inside the Model and return the formatted string instead of the raw date, you risk losing the ability to sort or filter by that date later in your application.

For example, if your view returns 07-01-2016 as a string, but your logic expects an object that can be sorted chronologically, you will encounter bugs when trying to order records. Always ensure that the data returned from the Model remains in its native format (e.g., Unix timestamp or ISO date string), and apply formatting only when rendering the HTML output.

Architectural Considerations

If you are migrating from CodeIgniter to a framework like Laravel, the handling of dates becomes even more streamlined thanks to native Carbon support in the global namespace. In such environments, accessing $user->start_date usually grants you a Carbon instance directly, allowing you to chain methods like format('d/m/Y') seamlessly.

For developers looking for more advanced date formatting options or if they find themselves repeating this logic across multiple views, consider creating a helper function in your App/Helpers.php. This centralizes the formatting rule (e.g., DD MM YYYY) so you don't have to remember specific format strings every time you render a date.

By keeping the separation between data storage and data presentation clear, you maintain a healthy codebase that is easier to debug and extend over time.

Our network