How to convert time format in codeigniter?

by lindsey.homenick , in category: PHP Frameworks , 2 months ago

How to convert time format in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , 2 months ago

@lindsey.homenick 

To convert time format in CodeIgniter, you can use PHP's date() function along with CodeIgniter's helper functions. Here is how you can do it:

  1. Load the Date Helper in your CodeIgniter controller:
1
$this->load->helper('date');


  1. Use the mdate() function to convert the time format. For example, if you want to convert a timestamp to a specific format, you can do it like this:
1
2
3
$timestamp = time(); // Current timestamp
$time_formatted = mdate('%h:%i %a', $timestamp); // Format the time
echo $time_formatted; // Output the formatted time


In the above example, the %h:%i %a format string represents hours, minutes, and AM/PM.


You can change the format string according to your needs. Here are some common format characters you can use:

  • %h: 12-hour format of an hour (01 to 12)
  • %i: Minutes with leading zeros (00 to 59)
  • %s: Seconds with leading zeros (00 to 59)
  • %a: Lowercase Ante meridiem and Post meridiem (am or pm)
  • %A: Uppercase Ante meridiem and Post meridiem (AM or PM)
  • %Y: A full numeric representation of a year (4 digits)
  • %m: Numeric representation of a month (01 to 12)
  • %d: Day of the month (01 to 31)


You can combine these format characters in any order to get the desired time format.


That's it! You have successfully converted the time format in CodeIgniter using the date() function.