How to change date format in PHP Codeigniter?

Today, We want to share with you codeigniter date format.In this post we will show you change date format yyyy-mm-dd (Y-m-d) to dd-mm-yyyy (d-m-Y), hear for use core php function strtotime() and date() for convert date format. we will give you demo and example for implement.In this post, we will learn about date format change in php with an example.

Display date format as DD MM YYYY in Codeigniter

Simple Example:


$sampleDate = '2021-01-02';
$convertDate = date("d-m-Y", strtotime($sampleDate));
  
print_r($convertDate);

Output:

01-02-2021

You can also create simple helper and repeatedly use it.

Helper Function:

if(!function_exists('getCurrentDate'))
{
    function getCurrentDate($format = 'd-m-Y', $originalDate)
    {
        return date($format, strtotime($originalDate));
    }
}

Use Helper Function:

<?php 
	echo getCurrentDate('d-m-Y',$user->created_at) 
?>

CodeIgniter Date helper

$this->load->helper(‘date’);

Functions of Date Helper

CustomDateGet.php

<?php
 class CustomDateGet extends CI_Controller
  {
     public function fetchCurrentActiveDt() 
     {  
        $this->load->helper('date');  
         echo "<title> Welcome To Pakainfo.com </title>"; 
       	 echo "<h2> USE of now() function </h2><br/>";
         echo now();  
 }
 ?> 

Using timezone_menu() Function

CustomDateGet.php

<?php
 class CustomDateGet extends CI_Controller
 {
	 public function fetchCurrentActiveDt() 
	 {  
	        $this->load->helper('date');  
	         echo "<title> Welcome To Pakainfo.com </title>"; 
			 echo "SET INDIA zone as default ";
			 echo timezone_menu('UP55');
			 echo "<br/> <br>";
			 echo timezone_menu();
	 }
 }
 ?>     

using timezones():

<?php
 class CustomDateGet extends CI_Controller
  {
 public function fetchCurrentActiveDt() 
     {  
        $this->load->helper('date');  
         echo "<title> Welcome To Pakainfo.com </title>"; 
 echo "timezone of india: ";
 echo timezones('UP55');
 }
 }
 ?>       

using date_range() Function:

<?php
 class CustomDateGet extends CI_Controller
  {
 public function fetchCurrentActiveDt() 
     {  
        $this->load->helper('date');  
         echo "<title> Welcome To Pakainfo.com </title>"; 
 $rangeBetween  = date_range('2021-01-05', '2021-01-26'); // SET RANGE
 echo "First 22 days between 5-26: ";
 foreach ($rangeBetween as $info)
 {
         echo " ".$info."\t ";
 }
 }
 }
 ?>       

using days_in_month() Function:

<?php
 class CustomDateGet extends CI_Controller
  {
 public function fetchCurrentActiveDt() 
     {   
        $this->load->helper('date');  
         echo "<title> Welcome To Pakainfo.com </title>"; 
		 echo "NUMBER OF DAYS in (February, 2021) is ";
		 echo days_in_month(02, 2021);
 }
 }
 ?>    

using timestamp() Function:

<?php
 class CustomDateGet extends CI_Controller
  {
 public function fetchCurrentActiveDt() 
     {  
        $this->load->helper('date');  
         echo "<title> Welcome To Pakainfo.com </title>"; 
		 $past_date = '1579564573';    
		 $past_first_dt = '1563666635';
		 $now = time(); 
		 $units = 3;       
		 $units2 = 5;      
		 echo "Display the last point of time till now ";
		 echo timespan($past_date, $now, $units); 
		 echo "<br> It display the time from 2021-07-21 05:20:35 pm till now ";
		 echo timespan($past_first_dt, $now, $units2); 
 }
 }
 ?>        

using nice_date() Function

<?php
 class CustomDateGet extends CI_Controller
  {
 public function fetchCurrentActiveDt() 
     {  
        $this->load->helper('date');  
         echo "<title> Welcome To Pakainfo.com </title>"; 
		 $fkedt1 = '20210122';
		 $fkedt2 = '1-22-2021'; 
		 $fkedt3 = '202101';
		 echo "This showing the bad format of date:"." first_dt"." ".$fkedt1."<br> date2"." ".$fkedt2. "<br> date3"." ".$fkedt3;
		 // It will show: 2021-01-22
		 echo "<br>";
		 $better_first_dt = nice_date($fkedt1, 'Y-m-d');
		 $better_date2 = nice_date($fkedt2, 'Y-m-d');
		 $better_date3 = nice_date($fkedt3, 'Y-m-d');
		 echo "Nice date Format" ." ".$better_first_dt. " <br> ".$better_date2."<br> ".$better_date3;
 }
 }
 ?>       

using human_to_unix() Function

<?php
 class CustomDateGet extends CI_Controller
  {
 public function fetchCurrentActiveDt() 
     {  
        $this->load->helper('date');  
         echo "<title> Welcome To Pakainfo.com </title>"; 
		 $now = time();
		 $humanReadable = unix_to_human($now);
		 echo "Human Readable Format of Date :YYYY-MM-DD HH:MM AM/PM: ";
		 echo ($humanReadable); 
		 echo "</br>";
		 $unixForm = human_to_unix($humanReadable); 
		 echo "In Unix timestamp:";
		 echo ($unixForm);}  
 }
 ?>        

I hope you get an idea about date format codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.