2

I'm trying to get the current datetime in codeigniter for my school project. I made a sample function in controller for this which looks like this:

public function sample(){
        date_default_timezone_set('Asia/Manila');
        echo date('y-m-d h:i:s');
    }

however, i found out that this outputs 20-01-20 04:52:19 instead of 20-01-20 16:52:19. any idea how to fix this ? thanks .

pj_seno
  • 83
  • 1
  • 6

3 Answers3

3

For 24 hour format of hours, use y-m-d H:i:s (uppercase H instead of lowercase h):

echo date('y-m-d H:i:s');   // Outputs: 20-01-20 17:04:00

As per the docs:

H 24-hour format of an hour with leading zeros 00 through 23

For PM times you would get the 24 hour format, and for AM times, you would get the leading zero (eg: 05:04:13).

If you prefer 24 format without leading zeros (16 for PM, 4 for AM), you can use G:

echo date('y-m-d G:i:s');   // Outputs for AM: 20-01-20 5:04:00 

// 12 hours later...
echo date('y-m-d G:i:s');   // Outputs for PM: 20-01-20 17:04:00

G 24-hour format of an hour without leading zeros 0 through 23

Brett Gregson
  • 5,867
  • 3
  • 42
  • 60
2

use H in stead of h

echo date('y-m-d H:i:s');
Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32
2

Use H instead of h like below:

echo date("y-m-d H:i:s");

For more help, you may read the original doc of php https://www.php.net/manual/en/function.date.php

Towsif
  • 337
  • 2
  • 12