Skip to content

Carbon

A PHP library that makes working with dates and times simpler than the built-in DateTime class.

Stars
16.6k
Forks
1.3k
Licence
MIT
Language
PHP
Last update
4 days ago
Type
Library

What it is

Carbon is a PHP library that extends PHP's built-in DateTime class with shorter, more readable methods. It lets developers write things like adding a day, comparing dates, or showing a time as '2 minutes ago'. The README also shows support for many languages when formatting dates and times.

  • Add or subtract days, weeks, or minutes from a date in one readable call
  • Calculate someone's age from a birth date
  • Show relative times like '2 minutes ago' and localize them into other languages
  • Format dates in a chosen locale using isoFormat
  • Freeze the current time during automated tests with setTestNow

Great for

  • PHP developers who work with dates and times often
  • Projects that need human-readable time differences such as '2 minutes ago'
  • Applications that display dates in several languages
  • Developers who want to mock the current time in tests

Not ideal for

  • Non-PHP developers, since it only works in the PHP language
  • People who only need a single simple date calculation and do not want an extra dependency
  • Projects that cannot use Composer or manual file loading
Step-by-step guide: how to use CarbonInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: PHP · Composer

    Install with Composer

    composer require nesbot/carbon
  2. 2

    Run it

    • Install the package with Composer using the command above.
    • Load Composer's autoloader with require 'vendor/autoload.php'.
    • Add 'use Carbon\Carbon;' at the top of your PHP file.
    • Run printf("Now: %s", Carbon::now()); to see the current date and time.
  3. 3

    Get the most out of it

    • Set a locale with ->locale('fr_FR') to format dates in other languages
    • Use diffForHumans() for relative times such as '2 minutes ago'
    • Use setTestNow() in tests to control what the current time is
    • Check the online documentation at carbonphp.github.io/carbon for the full method list

Downloads

Strengths

  • Short, readable method names compared with the plain DateTime class
  • Supports many languages and regional variants for date output
  • Installs easily with Composer and has an MIT license

Trade-offs

  • Only usable from PHP code
  • Adds a dependency to your project that you must keep updated
  • The README is brief, so detailed usage depends on the separate online documentation

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.