LoginSignup
11
9

More than 5 years have passed since last update.

Carbonでstrtotimeではなくstrftimeを使いたい

Posted at

概要

年を西暦ではなく和暦で表示したいという要望があったりしますよね。
そんな時はstrftimeを使っているかと思いますが、Carbonを導入してるからCarbonでという場合は formatLocalized() を使うとよいです。

コードサンプル

use Carbon\Carbon;

date_default_timezone_set('Asia/Tokyo');
setlocale(LC_TIME, 'ja_JP.utf8');

$format = '%Y年 (%EC%Ey年)';
$currentYear = Carbon::now()->formatLocalized($format);

// string(21) "2015年 (平成27年)"
var_dump($currentYear); 

備考

明治以降しか表示されません。

参考リンク

http://carbon.nesbot.com/
http://jp.php.net/strftime

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9