Skip to content

Instantly share code, notes, and snippets.

@localdisk
Created January 30, 2014 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save localdisk/8700693 to your computer and use it in GitHub Desktop.
Save localdisk/8700693 to your computer and use it in GitHub Desktop.
Carbon の使い方
<?php
// Carbon のインストール
// Composer 使います。使ってない人はそろそろ手を付けたほうがいいですよ
// composer.json にこう書いて…
// {
// "require": {
// "nesbot/Carbon": "*"
// }
// }
// composer install
require_once './vendor/autoload.php';
use Carbon\Carbon;
// 今
echo Carbon::now(); // 2014-01-30 09:34:53
echo "\n";
// 今日
echo Carbon::today(); // 2014-01-30 00:00:00
echo "\n";
// 昨日
echo Carbon::yesterday(); // 2014-01-29 00:00:00
echo "\n";
// 月初
echo Carbon::now()->startOfMonth(); // 2014-01-01 00:00:00
echo "\n";
// 月末
echo Carbon::now()->endOfMonth(); // 2014-01-31 23:59:59
echo "\n";
// 翌月末
// 下手に addMonth すると不具合が
echo Carbon::now()->addMonth(); // 2014-03-02 09:45:04  アイエエエ 3月 !? 3月 ナンデ!?
echo "\n";
// まず月初に戻って addMonth すると上手くいく
echo Carbon::now()->firstOfMonth()->addMonth()->endOfMonth(); // 2014-02-28 23:59:59
echo "\n";
// でも parse 使うのが一番楽かもしれない
echo Carbon::parse('last day of next month'); // 2014-02-28 09:37:58
echo "\n";
// diff もとれる
// twitter みたいなアレ
echo Carbon::now()->subMinutes(2)->diffForHumans(); // 2 minutes ago
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment