Home > front end >  How to get current DATE/TIME with microseconds in CI4?
How to get current DATE/TIME with microseconds in CI4?

Time:02-17

Awkward problem -> I'm trying to get current date&time in ISO 8601 format (like this: 2022-02-11 12:30:02.846108).

When I run $time = new Time('now', 'Europe/Amsterdam') in CI4, $time returns this:

object(CodeIgniter\I18n\Time)#79 (6) 
  { ["timezone":protected]=> object(DateTimeZone)#80 (2) 
    { ["timezone_type"]=> int(3) 
      ["timezone"]=> string(16) "Europe/Amsterdam" 
    } 
    ["locale":protected]=> string(2) "en" 
    ["toStringFormat":protected]=> string(19) "yyyy-MM-dd HH:mm:ss" 
    ["date"]=> string(26) "2022-02-11 12:30:02.846108" 
    ["timezone_type"]=> int(3) 
    ["timezone"]=> string(16) "Europe/Amsterdam" 
}

but when i try to get $time->date returns NULL.

Any ideea how to access date string in order to get the current time with microseconds?

CodePudding user response:

Found out there is no ->date attribute in the DateTime object, which is why $time->date returns NULL.

The proper way to get the date with microseconds is:

$time->format('Y-m-d\TH:i:s.u')
  • Related