How to Change Date Format in Codeigniter?
I would like to show you how to convert date format in php codeigniter 3 framework. we will change date format yyyy-mm-dd (Y-m-d) to dd-mm-yyyy (d-m-Y) with example.
we almost need to display date on any module or in detail page, so in this post i will give you simple example to change date formate in codeigniter. we will use core php function strtotime() and date() for convert date format.
i will share simple example to convert date format in codeigniter and also you can simply create helper function, so you can use same code repeatedly. So let's see bellow easy example.
Simple Example:
$sampleDate = '2019-01-02';
$convertDate = date("d-m-Y", strtotime($sampleDate));
print_r($convertDate);
Output:
01-02-2019
You can also create simple helper and repeatedly use it.
Helper Function:
if(!function_exists('changeDateFormat'))
{
function changeDateFormat($format = 'd-m-Y', $originalDate)
{
return date($format, strtotime($originalDate));
}
}
Use Helper Function:
<?php
echo changeDateFormat('d-m-Y',$user->created_at)
?>
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Codeigniter JQuery Ajax Request Example
- Codeigniter Resize Image and Create Thumbnail Example
- Codeigniter Drag and Drop Multiple Image Upload Example
- How to Get Current Controller or Method Name in Codeigniter?
- Codeigniter Dynamic Dependent Dropdown using Ajax Example
- Codeigniter JQuery Ajax Image Upload Example
- How to Implement Flash Messages in Codeigniter?
- Codeigniter Ajax Infinite Scroll Pagination Example
- Codeigniter Image Upload with Validation Example
- Codeigniter Ajax CRUD Tutorial Example
- Codeigniter Select2 Ajax Autocomplete from Database Example
- How to Get Current URL with Query String in Codeigniter?
- How to Get Current URL in Codeigniter?
- How to Get IP Address in Codeigniter?