Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

opencafe/datium

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

201 Commits

Repository files navigation

Datium

Build Status Scrutinizer Code Quality Latest Stable Version Total Downloads Join the chat at https://gitter.im/opencafe/datium npm

Awesome DateTime package written in PHP, with generalization support in calendar and translation, which makes Datium powerful and simple.

  • Simplicity in code and logic
  • Expandable in every part
  • Hijri, Jalali, Julian and Kurdish Calendars support

Support

  • ^PHP:5.4
  • PHP:7

Installation

Via Composer

composer require opencafe/datium

Manual

Just require Datium.php in your project and use Datium namespace as following example:

require_once 'src/Datium.php';
use OpenCafe\Datium;
echo Datium::now()->get();

Usage

Simply get what you want:

As datium output

Datium::now()->get(); // ex: 2016年01月01日 00:00:00
Datium::now()->timestamp(); // ex: 1420057800
Datium::now()->get('timestamp'); // ex: 1420057800

Or working with date as simple as you need:

Datium::now()->add('1 day')->get(); // ex: 2016年01月02日 00:00:00

Get

Get Datium as DateTime PHP object:

Datium::now()->object();

Or return it as simple date and time string:

Datium::now()->get();

And even with custom PHP YMD format:

Datium::now()->get( 'l jS F Y h:i:s A' );

Timestamp format:

Datium::create(2016,10,16)->get('timestamp');
// Result : 1476563400

Easy usage:

Datium::now()->all();
// Result
object(stdClass)#5 (6) {
 ["second"]=>
 string(2) "03"
 ["minute"]=>
 string(2) "10"
 ["hour"]=>
 string(2) "15"
 ["day"]=>
 string(2) "12"
 ["month"]=>
 string(2) "10"
 ["year"]=>
 string(4) "2016"
}
Datium::now()->all()->year; // 2016
Datium::now()->all()->month; // 10
Datium::now()->all()->day; // 12
Datium::now()->all()->hour; // 15
Datium::now()->all()->minute; // 10
Datium::now()->all()->second; // 03

Create

You can also simply create new time:

// Create with YMD
Datium::create( 2016, 1, 1, 12, 56, 13 )->get(); //ouput: 2016年01月01日 12:56:13
// Create with YMD without time
Datium::create( 2016, 1, 1 )->get(); // output: 2016年01月01日 12:56:13
// Create with timestamp
Datium::createTimestamp( 1420057800 )->get() // output => 2015年01月01日 00:00:00

Add Date

This method allow you add some year, month, week, day, hour, minute and second to current date.

// If current date is 2016年01月01日 00:00:00 then:
// Add 3 years
Datium::now()->add('3 year')->get();
// output => 2019年01月01日 00:00:00
// Add 1 week
Datium::create(2016, 1, 1)->add('1 week')->get();
// output => 2016年01月08日 00:00:00
// Add one month
Datium::now()->add('1 month')->get()
// output => 2016年02月01日 00:00:00
// Add 1 year, 3 month and 2 days
Datium::now()->add('1 year')
 ->add('3 month')
 ->add('2 day')
 ->add('1 hour')
 ->add('2 minute')
 ->add('3 second')
 ->get();
// output => 2017年04月03日 01:02:03

Sub Date

Sub some year, month, day, hour, minute and second from current date.

// If current date is 2016年01月01日 00:00:00 then:
// Sub 3 years
Datium::now()->sub('3 year')->get();
// output => 2013年01月01日 00:00:00
// Sub 1 week
Datium::create(2016, 1, 8)->sub('1 week')->get();
// output => 2016年01月01日 00:00:00
// Sub one month
Datium::now()->sub('1 month')->get()
// output => 2015年12月01日 00:00:00
// Sub 1 year, 3 month and 2 days
Datium::now()->sub('1 year')
 ->sub('3 month')
 ->sub('2 day')
 ->get();
// output => 2014年09月29日 00:00:00

Date Difference

This method will return the difference between two specific date with php date interval type.

// current generated date difference with next 5000 days
$diff = Datium::diff(
 Datium::now()->object(),
 Datium::now()->add('5000 day')->object()
);
echo $diff->days;
// output => 5000
echo $diff->year . ' year, ' . $diff->month . ' month, ' . $diff->day . ' day ';
// ouput => 13 year, 8 month, 7 day

Human readable time difference

Datium also supports human readable date and time difference.

// current generated date difference with next 5000 days
$diff = Datium::diff(
 Datium::now()->object(),
 Datium::now()->add('5000 day')->object()
)->simple->get();
// result => 13 years ago
// current generated date difference with next 5000 days
$diff = Datium::diff(
 Datium::now()->object(),
 Datium::now()->sub('5000 day')->object()
)->simple->get();
// result => 13 years remaining
// current generated date difference with next 5000 days
$diff = Datium::diff(
 Datium::now()->object(),
 Datium::now()->add('5000 day')->object()
)->simple->lang('fa')->get();
// result => ۱۳ سال پیش

Leap year

Define leap year of current year with generalization support.

// If current date was 2016
// Is 2016 a leap year?
Datium::now()->leap()->get();
// output => FALSE
// Is 2017 a leap year?
Datium::now()->add('1 year')->leap()->get();
// output => TRUE
Datium::now()->to('hijri')->leap()->get();

Get day of date

This method returns day of week or day of year with generalization support, you can add this feature to your custom calendars like other supported calendars in Datium.

Day of Year

What the day is in current year:

// If current date was 2015年09月03日
Datium::now()->dayOf()->year();
// output => 246
// Day of year to Gregorian
Datium::now()->dayOf()->year();
// Day of year to Jalali
Datium::now()->to( 'jalali' )->dayOf()->year();
// Day of year to Hijri
Datium::now()->to( 'hijri' )->dayOf()->year();
// Day of year to kurdish
Datium::now()->to( 'kurdish' )->dayOf()->year();

Day of Week

What day of week is current day:

// If current date 'll be 2015年09月09日 Thursday
Datium::now()->dayOf()->week();
// output => 5
// Day of week to Gregorian
Datium::now()->dayOf()->week();
// Day of week to Jalali
Datium::now()->to( 'jalali' )->dayOf()->week();
// Day of week to Hijri
Datium::now()->to( 'hijri' )->dayOf()->week();
// Day of week to Kurdish
Datium::now()->to( 'kurdish' )->dayOf()->week();

Last Day of Month

How many days is current month

// Last Day of Current Month to Gregorian
Datium::now()->dayOf()->lastDayMonth();
// Last Day of Current Month to Jalali
Datium::now()->to( 'jalali' )->dayOf()->lastDayMonth();
// Last Day of Current Month to Hijri
Datium::now()->to( 'hijri' )->dayOf()->lastDayMonth();
// Last Day of Current Month to Kurdish
Datium::now()->to( 'kurdish' )->dayOf()->lastDayMonth();

Generalization

Calendar generalization

Datium supports calendar generalization, you can add customized calendars to Datium and used them as it's own default calendars.

Datium::create( 2015, 11, 9 )->to( 'jalali' )->get()
//Convert Gregorian to Jalali calendar: 1394年08月18日 00:00:00
Datium::create( 2015, 11, 9 )->to( 'hijri' )->get()
//Convert Gregorian to Hijri calendar: 1437年01月26日 00:00:00
Datium::create( 2015, 11, 9 )->to( 'kurdish' )->get()
//Convert Gregorian to Kurdish calendar: 2715年08月18日 00:00:00

Convert all calendars which supported on Datium or event your customized calendars as simple as possible:

Datium::create( 1395, 7, 25 )->from( 'jalali' )->get(); // Gregorian is default value for destination calendar.
// result: 2016年10月16日 00:00:00
Datium::create( 1395, 7, 25 )->from( 'jalali' )->to( 'hijri' )->get();
// result: 1438年01月14日 00:00:00

Translation Generalization

Generalization in translation is another Datium generalization support.

Datium::create( 2016, 6, 25, 12, 0, 0 )->to( 'jalali' )->lang( 'fa' )->get('l jS F Y h:i:s A');
// ex: شنبه ۵ تیر ۱۳۹۵ ۱۲:۰۰:۰۰ ب.ظ
Datium::create( 2016, 6, 25, 12, 0, 0 )->to( 'jalali' )->get('l jS F Y h:i:s A');
// ex: Shanbe 5th Tir 1395 12:00:00 PM
Datium::create(2016, 6, 25, 12, 0, 0)->to('hijri')->get('l jS F Y h:i:s A');
// ex: as-Sabt 19th Ramadan 1437 12:00:00 PM
Datium::create(2016, 6, 25, 12, 0, 0)->to('kurdish')->get('l jS F Y h:i:s A');
// ex: Şeme 5th Puşper 2716 12:00:00 PM
Datium::create(2016, 6, 25, 12, 0, 0)->get('l jS F Y h:i:s A');
// ex: Saturday 25th June 2016 12:00:00 PM

Change Configuration

You can change any configuration after initialize Datium object.

$datium = Datium::create(
 $date->format('Y'),
 $date->format('m'),
 $date->format('d'),
 $date->format('h'),
 $date->format('i'),
 $date->format('s')
 );
$datium->setConfig(['timezone'=>'Europe/Istanbul']);

Default configuration

[
 'timezone' => 'Asia/Tehran',
 'language' => 'en',
 'default_calendar' => 'gregorian',
 'date_interval' => [ 'D', 'M', 'Y', 'HT', 'MT', 'ST' ],
 'date_simple' => [ 'day', ' month', ' year', ' hour', ' minute', ' second' ],
]

AltStyle によって変換されたページ (->オリジナル) /