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:

created_at) 
?>

CodeIgniter Date helper

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

Functions of Date Helper

CustomDateGet.php

load->helper('date');  
         echo " Welcome To Pakainfo.com "; 
       	 echo "

USE of now() function


";         echo now();  } ?>

Using timezone_menu() Function

CustomDateGet.php

load->helper('date');  
	         echo " Welcome To Pakainfo.com "; 
			 echo "SET INDIA zone as default ";
			 echo timezone_menu('UP55');
			 echo "

"; echo timezone_menu(); } } ?>

using timezones():


load->helper('date');  
         echo " Welcome To Pakainfo.com "; 
 echo "timezone of india: ";
 echo timezones('UP55');
 }
 }
 ?>       

using date_range() Function:

load->helper('date');  
         echo " Welcome To Pakainfo.com "; 
 $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:

load->helper('date');  
         echo " Welcome To Pakainfo.com "; 
		 echo "NUMBER OF DAYS in (February, 2021) is ";
		 echo days_in_month(02, 2021);
 }
 }
 ?>    

using timestamp() Function:

load->helper('date');  
         echo " Welcome To Pakainfo.com "; 
		 $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 "
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

load->helper('date');  
         echo " Welcome To Pakainfo.com "; 
		 $fkedt1 = '20210122';
		 $fkedt2 = '1-22-2021'; 
		 $fkedt3 = '202101';
		 echo "This showing the bad format of date:"." first_dt"." ".$fkedt1."
date2"." ".$fkedt2. "
date3"." ".$fkedt3; // It will show: 2021-01-22 echo "
"; $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. "
".$better_date2."
".$better_date3; } } ?>

using human_to_unix() Function

load->helper('date');  
         echo " Welcome To Pakainfo.com "; 
		 $now = time();
		 $humanReadable = unix_to_human($now);
		 echo "Human Readable Format of Date :YYYY-MM-DD HH:MM AM/PM: ";
		 echo ($humanReadable); 
		 echo "
"; $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.

Leave a Comment