How to change Date format in codeigniter?

Date format function  

File name : Change_Date_Format.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Change_Date_Format extends CI_Controller {
public function __construct() {
parent::__construct();
//$this->load->database();

}


public function index()
{
//$sampleDate = '2019-01-02';
$sampleDate = date("Y-m-d");
$convertDate = date("d-m-Y", strtotime($sampleDate));

print_r($convertDate);
}


}


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)
?>

How to set Timezone of asia.

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
date_default_timezone_set('Asia/Kolkata');


If you are looking globabally setup timezone in whole project then you can setup it CI application/config.php file

$config['time_reference'] = 'Asia/Dubai';

Add this line to autoload.php in the application folder:
$autoload['time_zone'] = date_default_timezone_set('Asia/Kolkata');

How to store date & Time in db Am Pm Format.

File Name : Controller

<?php defined('BASEPATH') OR exit('no direct script access allowed');
class Datedemo extends CI_Controller
{
public function __construct()
{
parent::__construct();
}

public function index()
{
$this->load->view('datetime');

}

public function insertdata()
{

date_default_timezone_set('Asia/Kolkata');
//$date = date('Y-m-d h:i:s');
$date = date("d-m-Y H:i:s");
//$time = time('H:i:s');

//echo date('h:i:s a m/d/Y', strtotime($date));
$time = date('h:i:s a', strtotime($date));

$data = array(
'date' => $date,
'time' => $time
);


$result = $this->Auth_model->datainsert($data);
}


}

How to Change Date format and show on view Page in Codeigniter?

<td> <?php

if(date('Y-m-d') == date('Y-m-d',strtotime($row->created_at))){

echo date('h:i A',strtotime($row->created_at));

}else{

//echo $row->created_at !="" && $row->created_at !="0000-00-00 00:00:00" ? date('d/m/Y h:i A',strtotime($row->created_at)):'';
echo date('d/m/Y h:i A',strtotime($row->created_at));
}
?></td>

Date function in php

File Name :

<?php
$timestamp = strtotime( "February 15, 2015" );
print date( 'Y-m-d', $timestamp );
?>


Output : 2015-02-15

File Name :

<?php
// created_on = 2020-10-06 18:14:09
// date this format
$input = $allopenappl['created_on'];
$date = strtotime($input);
echo date('d-m-Y',$date);
?>


output : 06-10-2020

File Name :

<?php echo date("H:i:s"); ?>

File Name :

<?php
echo date("d/m/Y") . "<br>";
echo date("d-m-Y") . "<br>";
echo date("d.m.Y");
?>


Output :
02/03/2021
02-03-2021
02.03.2021

Return current date and time from the remote server

File Name :

<?php
// Return current date and time from the remote server
echo date("h:i:s") . "<br>";
echo date("F d, Y h:i:s A") . "<br>";
echo date("h:i a");
?>


Output :
10:53:44
March 02, 2021 10:53:44 AM
10:53 am

h - Represent hour in 12-hour format with leading zeros (01 to 12)
H - Represent hour in in 24-hour format with leading zeros (00 to 23)
i - Represent minutes with leading zeros (00 to 59)
s - Represent seconds with leading zeros (00 to 59)
a - Represent lowercase ante meridiem and post meridiem (am or pm)
A - Represent uppercase Ante meridiem and Post meridiem (AM or PM)

Convert Timestamp to Human Readable Format

File Name :

<?php
/* Return current date and time
from the remote server as timestamp
*/
$timestamp = time();
echo($timestamp);
echo "<br>";

// Converting timestamp to human readable format
echo(date("F d, Y h:i:s", $timestamp));
?>

File Name :


File Name :

<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>

How to get days between two date.

File Name :

$dt1 = date_create($row->departure_date);
$dt2 = date_create($row->arival_date);
$diff = date_diff($dt1,$dt2);
echo $diff->format("%R%a days");

The PHP mktime() Function

The mktime() function is used to create the timestamp based on a specific date and time. If no date and time is provided, the timestamp for the current date and time is returned.

File Name :

mktime(hour, minute, second, month, day, year)

Example :-

File Name :

The following example displays the timestamp corresponding to 3:20:12 pm on May 10, 2014:

<?php
// Create the timestamp for a particular date
echo mktime(15, 20, 12, 5, 10, 2014);
?>

output :
1399735212

Date Difference

File Name :

<?php
$date1 = date('Y-m-d', strtotime($dt_res['created_at']));
$date2 = date('Y-m-d');

$diff = abs(strtotime($date2)-strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

if($years!=0 && $months!=0 && $days!=0){
$time_elapsed=$years.' Years '.$months.' Months '.$days.' Days ';
}
else if($years==0 && $months!=0 && $days!=0){
$time_elapsed=$years.' Years '.$months.' Months '.$days.' Days ';
}
else if($years==0 && $months==0 && $days!=0){
$time_elapsed=$days.' Days ';
}
else if($years==0 && $months!=0 && $days==0){
$time_elapsed=$months.' Months ';
}
else if($years!=0 && $months!=0 && $days==0){
$time_elapsed=$years.' Years '.$months.' Months ';
}
else if($years!=0 && $months==0 && $days!=0){
$time_elapsed=$years.' Years '.$days.' Days ';
}
else {
$time_elapsed='------';
}
echo $time_elapsed;
?>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here