Home > Back-end >  Get location date format
Get location date format

Time:05-09

How can I get the date format of the user's country through the Carbon library? For example, the code below brings me the iso formats of that region.

 Carbon::now()->locale('tr_TR')->getIsoFormats();

This code gives the ISO format used in that region.

array:6 [
  "LT" => "HH:mm"
  "LTS" => "HH:mm:ss"
  "L" => "DD.MM.YYYY"
  "LL" => "D MMMM YYYY"
  "LLL" => "D MMMM YYYY HH:mm"
  "LLLL" => "dddd, D MMMM YYYY HH:mm"
]

What function should I use so that I can output it as d.m.Y instead of "L" => "DD.MM.YYYY" instead of ISO format.

CodePudding user response:

here you can check what Carbon does for each ISO format:

https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Traits/Date.php#L1907

Some of them have equivalent in DateTime::format if this is the format you expect as an output but all won't have an equivalent code. For instance DateTime::format has a format for the ordinal st / nd / th but it's only in English, while the ISO format Do means day number with ordinal of the current language (can be inci for tr_TR locale).

This means you can make an approximated mapping of what ISO format is for DateTime::format, but this won't be an exact match.

CodePudding user response:

I believe you can use l as your format like this.

$date = Carbon::parse('2022-05-15')->locale('tr_TR');
echo $date->isoFormat('l');
  • Related