Convert Figma logo to code with AI

jenssegers logodate

🗓 A library to help you work with dates in multiple languages, based on Carbon.

1,814
244
1,814
6

Top Related Projects

16,515

A simple PHP API extension for DateTime.

47,953

Parse, validate, manipulate, and display dates in javascript.

46,619

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

34,434

⏳ Modern JavaScript date utility library ⌛️

1,589

Complex period comparisons

Quick Overview

Jenssegers/date is a PHP library that extends Carbon to add multi-language support for date and time formatting. It provides an easy way to localize dates and times in various languages, making it particularly useful for multilingual applications.

Pros

  • Seamless integration with Laravel and other PHP frameworks
  • Supports a wide range of languages and locales
  • Easy to use and extend with custom translations
  • Maintains compatibility with Carbon's API

Cons

  • Requires PHP 7.2 or higher, which may not be suitable for older projects
  • Some less common languages or dialects may have incomplete translations
  • Potential performance overhead when dealing with a large number of date operations

Code Examples

Creating a localized date:

use Jenssegers\Date\Date;

Date::setLocale('fr');
$date = new Date('2023-04-15');
echo $date->format('l j F Y'); // Samedi 15 avril 2023

Relative time formatting:

use Jenssegers\Date\Date;

Date::setLocale('es');
$date = new Date('-3 days');
echo $date->diffForHumans(); // hace 3 días

Switching locales on the fly:

use Jenssegers\Date\Date;

$date = new Date('2023-04-15');
echo $date->format('F', 'de'); // April
echo $date->format('F', 'fr'); // avril

Getting Started

  1. Install the library using Composer:
composer require jenssegers/date
  1. Use the library in your PHP code:
use Jenssegers\Date\Date;

// Set the default locale
Date::setLocale('fr');

// Create a new date
$date = new Date();

// Format the date
echo $date->format('l j F Y'); // e.g., "samedi 15 avril 2023"

Competitor Comparisons

16,515

A simple PHP API extension for DateTime.

Pros of Carbon

  • More comprehensive and feature-rich date/time manipulation library
  • Extensive documentation and community support
  • Immutable objects for safer date handling

Cons of Carbon

  • Larger codebase and potentially higher memory footprint
  • Steeper learning curve due to more extensive API

Code Comparison

Carbon:

$date = Carbon::now()->addDays(7);
$formatted = $date->format('Y-m-d H:i:s');

Date:

$date = Date::now()->addDays(7);
$formatted = $date->format('Y-m-d H:i:s');

Both libraries offer similar syntax for basic date manipulation, but Carbon provides more advanced features and methods for complex operations.

Key Differences

  • Carbon focuses on being a comprehensive date/time library, while Date aims to be a lightweight alternative
  • Carbon offers more localization options and timezone handling capabilities
  • Date extends PHP's DateTime class, making it more familiar to developers used to native PHP date functions

Use Cases

  • Carbon: Ideal for projects requiring extensive date/time manipulation and formatting
  • Date: Suitable for simpler projects or when a lightweight solution is preferred

Both libraries are well-maintained and popular choices for PHP developers, with the choice depending on project requirements and complexity.

47,953

Parse, validate, manipulate, and display dates in javascript.

Pros of Moment

  • Extensive documentation and community support
  • Wider range of date manipulation and formatting options
  • Better internationalization support with locales

Cons of Moment

  • Larger bundle size, which can impact performance
  • No longer actively maintained (in legacy mode)
  • More complex API compared to simpler alternatives

Code Comparison

Date:

use Jenssegers\Date\Date;

$date = Date::now()->addWeeks(2);
echo $date->format('Y-m-d');

Moment:

const moment = require('moment');

const date = moment().add(2, 'weeks');
console.log(date.format('YYYY-MM-DD'));

Summary

Date is a PHP library that extends Carbon, providing localized date/time handling. It's lightweight and easy to use, making it suitable for PHP projects requiring basic date manipulation.

Moment is a more comprehensive JavaScript library for parsing, validating, manipulating, and formatting dates. It offers extensive features but comes with a larger footprint and is no longer actively maintained.

Choose Date for PHP projects needing simple, localized date handling. Opt for Moment in JavaScript applications requiring advanced date operations, but consider alternatives due to its legacy status.

46,619

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

Pros of Day.js

  • Lightweight and minimalist (only 2KB minified and gzipped)
  • Immutable and chainable API, similar to Moment.js
  • Extensive plugin system for additional functionality

Cons of Day.js

  • Less comprehensive localization support compared to Date
  • Fewer built-in features, requiring plugins for advanced functionality

Code Comparison

Date:

use Jenssegers\Date\Date;

Date::now()->format('Y-m-d');
Date::parse('-1 day')->diffForHumans();

Day.js:

import dayjs from 'dayjs'

dayjs().format('YYYY-MM-DD')
dayjs().subtract(1, 'day').fromNow()

Key Differences

  • Date is primarily for PHP, while Day.js is for JavaScript
  • Date extends Carbon and provides additional localization features
  • Day.js focuses on a small core with optional plugins for extended functionality

Use Cases

  • Date: Ideal for PHP projects requiring extensive date manipulation and localization
  • Day.js: Perfect for lightweight JavaScript applications with basic date operations

Community and Maintenance

  • Date: Smaller community, less frequent updates
  • Day.js: Larger community, more active development and frequent releases
34,434

⏳ Modern JavaScript date utility library ⌛️

Pros of date-fns

  • Modular architecture allows for tree-shaking and smaller bundle sizes
  • Extensive API with more functions for date manipulation and formatting
  • Immutable by design, preventing unintended side effects

Cons of date-fns

  • Steeper learning curve due to its larger API surface
  • Requires more setup and configuration for optimal use
  • May be overkill for simple date operations

Code Comparison

date:

use Jenssegers\Date\Date;

$date = new Date('2023-05-15');
echo $date->format('Y-m-d'); // 2023-05-15
echo $date->addDays(7)->format('Y-m-d'); // 2023-05-22

date-fns:

import { format, addDays } from 'date-fns';

const date = new Date('2023-05-15');
console.log(format(date, 'yyyy-MM-dd')); // 2023-05-15
console.log(format(addDays(date, 7), 'yyyy-MM-dd')); // 2023-05-22

Both libraries provide similar functionality for basic date operations, but date-fns offers a more functional approach with separate functions for each operation. date, on the other hand, uses a more object-oriented style with method chaining. The choice between the two depends on the specific project requirements, programming language preferences, and the complexity of date manipulations needed.

1,589

Complex period comparisons

Pros of Period

  • Focuses specifically on date ranges and periods, offering more specialized functionality
  • Provides methods for working with boundaries, gaps, and overlaps between periods
  • Supports custom implementations of periods for different use cases

Cons of Period

  • More limited in scope compared to Date's broader date/time manipulation features
  • Lacks localization support for different languages and regions
  • Doesn't offer as many formatting options for date output

Code Comparison

Date:

$date = Date::now()->addWeeks(2);
$formatted = $date->format('Y-m-d');
$diffInDays = $date->diffInDays('2023-12-31');

Period:

$period = Period::make('2023-01-01', '2023-12-31');
$overlaps = $period->overlapsWith(Period::make('2023-06-01', '2024-01-01'));
$subPeriods = $period->splitByMonth();

Date provides a more general-purpose date manipulation library, while Period specializes in working with date ranges and periods. Date offers a wider range of features for formatting, localization, and general date/time operations. Period excels in handling specific use cases involving date ranges, such as checking for overlaps, gaps, and manipulating periods as a whole.

Choose Date for broader date/time functionality or Period for specialized date range operations, depending on your project's specific requirements.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Date

Latest Stable Version Total Downloads Build Status Coverage Status Donate

This date library extends Carbon with multi-language support. Methods such as format, diffForHumans, parse, createFromFormat and the new timespan, will now be translated based on your locale.

All translations made by contributors have been moved to the Carbon 2 package. This package now uses the Carbon translations to provide you with better multi-language support. Translation issues should be reported on the Carbon repository. Please also check out the original documentation here.

Installation

Install using composer:

composer require jenssegers/date

Laravel

There is a service provider included for integration with the Laravel framework. This provider will get the application locale setting and use this for translations. This service will be automatically registered if you use Laravel 5.5+ using the auto-discovery. Else to register the service provider, add the following to the providers array in config/app.php:

Jenssegers\Date\DateServiceProvider::class,

You can also add it as a Facade in config/app.php:

'Date' => Jenssegers\Date\Date::class,

Languages

This package contains language files for the following languages (https://carbon.nesbot.com/docs/#supported-locales):

  • Afar (aa)
  • Afrikaans (af)
  • Aghem (agq)
  • Aguaruna (agr)
  • Akan (ak)
  • Amharic (am)
  • Aragonese (an)
  • Angika (anp)
  • Arabic (ar)
  • Assamese (as)
  • Asu (asa)
  • Asturian (ast)
  • Southern Aymara (ayc)
  • Azerbaijani (az)
  • Basaa (bas)
  • Belarusian (be)
  • Bemba (bem)
  • ber (ber)
  • Bena (bez)
  • Bulgarian (bg)
  • Bhili (bhb)
  • Bhojpuri (bho)
  • Bislama (bi)
  • Bambara (bm)
  • Bengali (bn)
  • Tibetan (bo)
  • Breton (br)
  • Bodo (brx)
  • Bosnian (bs)
  • Bilin (byn)
  • Catalan (ca)
  • Chakma (ccp)
  • Chechen (ce)
  • Chiga (cgg)
  • Cherokee (chr)
  • Chinese (cmn)
  • Crimean Turkish (crh)
  • Czech (cs)
  • Kashubian (csb)
  • Church Slavic (cu)
  • Chuvash (cv)
  • Welsh (cy)
  • Danish (da)
  • Taita (dav)
  • German (de)
  • Zarma (dje)
  • Dogri (macrolanguage) (doi)
  • Lower Sorbian (dsb)
  • Duala (dua)
  • Divehi (dv)
  • Jola-Fonyi (dyo)
  • Dzongkha (dz)
  • Embu (ebu)
  • Ewe (ee)
  • Greek (modern) (el)
  • English (en)
  • Esperanto (eo)
  • Spanish (es)
  • Estonian (et)
  • Basque (eu)
  • Ewondo (ewo)
  • Persian (fa)
  • Fulah (ff)
  • Finnish (fi)
  • Filipino (fil)
  • Faroese (fo)
  • French (fr)
  • Friulian (fur)
  • Western Frisian (fy)
  • Irish (ga)
  • Gaelic (gd)
  • Geez (gez)
  • Galician (gl)
  • Konkani (gom)
  • Swiss German (gsw)
  • Gujarati (gu)
  • Gusii (guz)
  • Manx (gv)
  • Hausa (ha)
  • Hakka Chinese (hak)
  • Hawaiian (haw)
  • Hebrew (modern) (he)
  • Hindi (hi)
  • Fiji Hindi (hif)
  • Chhattisgarhi (hne)
  • Croatian (hr)
  • Upper Sorbian (hsb)
  • Haitian (ht)
  • Hungarian (hu)
  • Armenian (hy)
  • i18n (i18n)
  • Interlingua (ia)
  • Indonesian (id)
  • Igbo (ig)
  • Sichuan Yi (ii)
  • Inupiaq (ik)
  • in (in)
  • Icelandic (is)
  • Italian (it)
  • Inuktitut (iu)
  • iw (iw)
  • Japanese (ja)
  • Ngomba (jgo)
  • Machame (jmc)
  • Javanese (jv)
  • Georgian (ka)
  • Kabyle (kab)
  • Kamba (kam)
  • Makonde (kde)
  • Kabuverdianu (kea)
  • Koyra Chiini (khq)
  • Kikuyu (ki)
  • Kazakh (kk)
  • Kako (kkj)
  • Kalaallisut (kl)
  • Kalenjin (kln)
  • Central Khmer (km)
  • Kannada (kn)
  • Korean (ko)
  • Konkani (kok)
  • Kashmiri (ks)
  • Shambala (ksb)
  • Bafia (ksf)
  • Colognian (ksh)
  • Kurdish (ku)
  • Cornish (kw)
  • Kirghiz (ky)
  • Langi (lag)
  • Luxembourgish (lb)
  • Ganda (lg)
  • Limburgan (li)
  • Ligurian (lij)
  • Lakota (lkt)
  • Lingala (ln)
  • Lao (lo)
  • Northern Luri (lrc)
  • Lithuanian (lt)
  • Luba-Katanga (lu)
  • Luo (luo)
  • Luyia (luy)
  • Latvian (lv)
  • Literary Chinese (lzh)
  • Magahi (mag)
  • Maithili (mai)
  • Masai (mas)
  • Meru (mer)
  • Morisyen (mfe)
  • Malagasy (mg)
  • Makhuwa-Meetto (mgh)
  • Metaʼ (mgo)
  • Eastern Mari (mhr)
  • Maori (mi)
  • Mískito (miq)
  • Karbi (mjw)
  • Macedonian (mk)
  • Malayalam (ml)
  • Mongolian (mn)
  • Manipuri (mni)
  • mo (mo)
  • Marathi (mr)
  • Malay (ms)
  • Maltese (mt)
  • Mundang (mua)
  • Burmese (my)
  • Mazanderani (mzn)
  • Min Nan Chinese (nan)
  • Nama (naq)
  • Norwegian BokmÃ¥l (nb)
  • North Ndebele (nd)
  • Low German (nds)
  • Nepali (ne)
  • Central Nahuatl (nhn)
  • Niuean (niu)
  • Dutch (nl)
  • Kwasio (nmg)
  • Norwegian Nynorsk (nn)
  • Ngiemboon (nnh)
  • Norwegian (no)
  • South Ndebele (nr)
  • Northern Sotho (nso)
  • Nuer (nus)
  • Nyankole (nyn)
  • Occitan (oc)
  • Oromo (om)
  • Oriya (or)
  • Ossetian (os)
  • Panjabi (pa)
  • Papiamento (pap)
  • Polish (pl)
  • Prussian (prg)
  • Pashto (ps)
  • Portuguese (pt)
  • Quechua (qu)
  • Cusco Quechua (quz)
  • Rajasthani (raj)
  • Romansh (rm)
  • Rundi (rn)
  • Romanian (ro)
  • Rombo (rof)
  • Russian (ru)
  • Kinyarwanda (rw)
  • Rwa (rwk)
  • Sanskrit (sa)
  • Sakha (sah)
  • Samburu (saq)
  • Santali (sat)
  • Sangu (sbp)
  • Sardinian (sc)
  • Sindhi (sd)
  • Northern Sami (se)
  • Sena (seh)
  • Koyraboro Senni (ses)
  • Sango (sg)
  • Samogitian (sgs)
  • sh (sh)
  • Tachelhit (shi)
  • Shan (shn)
  • Shuswap (shs)
  • Sinhala (si)
  • Sidamo (sid)
  • Slovak (sk)
  • Slovene (sl)
  • Samoan (sm)
  • Inari Sami (smn)
  • Shona (sn)
  • Somali (so)
  • Albanian (sq)
  • Serbian (sr)
  • Swati (ss)
  • Southern Sotho (st)
  • Swedish (sv)
  • Swahili (sw)
  • Silesian (szl)
  • Tamil (ta)
  • Tulu (tcy)
  • Telugu (te)
  • Teso (teo)
  • Tetum (tet)
  • Tajik (tg)
  • Thai (th)
  • Chitwania Tharu (the)
  • Tigrinya (ti)
  • Tigre (tig)
  • Turkmen (tk)
  • Tagalog (tl)
  • Klingon (tlh)
  • Tswana (tn)
  • Tongan (Tonga Islands) (to)
  • Tok Pisin (tpi)
  • Turkish (tr)
  • Tsonga (ts)
  • Tatar (tt)
  • Tasawaq (twq)
  • Talossan (tzl)
  • Tamazight (tzm)
  • Uighur (ug)
  • Ukrainian (uk)
  • Unami (unm)
  • Urdu (ur)
  • Uzbek (uz)
  • Vai (vai)
  • Venda (ve)
  • Vietnamese (vi)
  • Volapük (vo)
  • Vunjo (vun)
  • Walloon (wa)
  • Walser (wae)
  • Wolaytta (wal)
  • Wolof (wo)
  • Xhosa (xh)
  • Soga (xog)
  • Yangben (yav)
  • Yiddish (yi)
  • Yoruba (yo)
  • Cantonese (yue)
  • Yau (Morobe Province) (yuw)
  • Standard Moroccan Tamazight (zgh)
  • Chinese (zh)
  • Zulu (zu)

Usage

The Date class extends the Carbon methods such as format and diffForHumans, and translates them based on your locale:

use Jenssegers\Date\Date;

Date::setLocale('nl');

echo Date::now()->format('l j F Y H:i:s'); // zondag 28 april 2013 21:58:16

echo Date::parse('-1 day')->diffForHumans(); // 1 dag geleden

The Date class also added some aliases and additional methods such as: ago which is an alias for diffForHumans, and the timespan method:

echo $date->timespan(); // 3 months, 1 week, 1 day, 3 hours, 20 minutes

Methods such as parse and createFromFormat also support "reverse translations". When calling these methods with translated input, it will try to translate it to English before passing it to DateTime:

$date = Date::createFromFormat('l d F Y', 'zaterdag 21 maart 2015');

Carbon

Carbon is the library the Date class is based on. All of the original Carbon operations are still available, check out https://carbon.nesbot.com/docs for more information. Here are some of the available methods:

Creating dates

You can create Date objects just like the DateTime object (http://www.php.net/manual/en/datetime.construct.php):

$date = new Date();
$date = new Date('2000-01-31');
$date = new Date('2000-01-31 12:00:00');

// With time zone
$date = new Date('2000-01-31', new DateTimeZone('Europe/Brussels'));

You can skip the creation of a DateTimeZone object:

$date = new Date('2000-01-31', 'Europe/Brussels');

Create Date objects from a relative format (http://www.php.net/manual/en/datetime.formats.relative.php):

$date = new Date('now');
$date = new Date('today');
$date = new Date('+1 hour');
$date = new Date('next monday');

This is also available using these static methods:

$date = Date::parse('now');
$date = Date::now();

Creating a Date from a timestamp:

$date = new Date(1367186296);

Or from an existing date or time:

$date = Date::createFromDate(2000, 1, 31);
$date = Date::createFromTime(12, 0, 0);
$date = Date::create(2000, 1, 31, 12, 0, 0);

Formatting Dates

You can format a Date object like the DateTime object (http://www.php.net/manual/en/function.date.php):

echo Date::now()->format('Y-m-d'); // 2000-01-31

The Date object can be cast to a string:

echo Date::now(); // 2000-01-31 12:00:00

Get a human readable output (alias for diffForHumans):

echo $date->ago(); // 5 days ago

Calculate a timespan:

$date = new Date('+1000 days');
echo Date::now()->timespan($date);
// 2 years, 8 months, 3 weeks, 5 days

// or even
echo Date::now()->timespan('+1000 days');

Get years since date:

$date = new Date('-10 years');
echo $date->age; // 10

$date = new Date('+10 years');
echo $date->age; // -10

Manipulating Dates

You can manipulate by using the add and sub methods, with relative intervals (http://www.php.net/manual/en/datetime.formats.relative.php):

$yesterday = Date::now()->sub('1 day');
$tomorrow  = Date::now()->add('1 day');

// ISO 8601
$date = Date::now()->add('P2Y4DT6H8M');

You can access and modify all date attributes as an object:

$date->year = 2013:
$date->month = 1;
$date->day = 31;

$date->hour = 12;
$date->minute = 0;
$date->second = 0;

Contributing

Language contributions should made to https://github.com/briannesbitt/Carbon.

License

Laravel Date is licensed under The MIT License (MIT).

Security contact information

To report a security vulnerability, follow these steps.