PHP code example of andegna / calender

1. Go to this page and download the library: Download andegna/calender library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andegna / calender example snippets


// create a gregorian date using PHP's built-in DateTime class
$gregorian = new DateTime('next monday');

// just pass it to Andegna\DateTime constractor and you will get $ethiopian date
$ethipic = new Andegna\DateTime($gregorian);

// format it
// ሰኞ፣ ግንቦት ፯ ቀን (ሥላሴ) 00:00:00 እኩለ፡ሌሊት EAT ፳፻፱ (ማርቆስ) ዓ/ም
echo $ethipic->format(Andegna\Constants::DATE_GEEZ_ORTHODOX);

// ሰኞ፣ ግንቦት 07 ቀን 00:00:00 እኩለ፡ሌሊት EAT 2009 ዓ/ም
echo $ethipic->format(Andegna\Constants::DATE_ETHIOPIAN);

$ethipic->modify('+8 hours');
$ethipic->sub(new DateInterval('PT30M')); // 30 minutes

// ሰኞ, 07-ግን-2009 07:30:00 EAT
echo $ethipic->format(DATE_COOKIE);

echo $ethipic->getYear();   // 2009
echo $ethipic->getMonth();  // 9
echo $ethipic->getDay();    // 7

echo $ethipic->getTimestamp(); // 1494822600

// turn it back to gregorian 
// Monday, 15-May-2017 07:30:00 EAT
echo $ethipic->toGregorian()->format(DATE_COOKIE);

$millennium = Andegna\DateTimeFactory::of(2000, 1, 1);

// ረቡዕ፣ መስከረም 01 ቀን (ልደታ) 00:00:00 እኩለ፡ሌሊት EAT 2000 (ማቴዎስ) ዓ/ም
echo $millennium->format(\Andegna\Constants::DATE_ETHIOPIAN_ORTHODOX).PHP_EOL;

// Wednesday, 12-Sep-2007 00:00:00 EAT
echo $millennium->toGregorian()->format(DATE_COOKIE).PHP_EOL;

$fall_of_derg = Andegna\DateTimeFactory::of(1983, 9, 20, 7, 43, 21, new DateTimeZone('Africa/Addis_Ababa'));

// ማክሰኞ፣ ግንቦት 20 ቀን (ሕንፅተ) 07:43:21 ጡዋት EAT 1983 (ዮሐንስ) ዓ/ም
echo $fall_of_derg->format(\Andegna\Constants::DATE_ETHIOPIAN_ORTHODOX).PHP_EOL;

// Tuesday, 28-May-1991 07:43:21 EAT
echo $fall_of_derg->toGregorian()->format(DATE_COOKIE).PHP_EOL;

$timestamp = time(); // or some other place ¯\_(ツ)_/¯

$ethipic = Andegna\DateTimeFactory::fromTimestamp($timestamp);

$sheger = new DateTimeZone('Africa/Addis_Ababa');

$ethiopic = Andegna\DateTimeFactory::fromTimestamp($timestamp, $sheger);

$gregorian = new DateTime('Thu, 11 May 2017 19:01:26 GMT');
$ethiopic = Andegna\DateTimeFactory::fromDateTime($gregorian);

// or just pass it through the constractor
$ethiopic = new Andegna\DateTime(new DateTime('next sunday'));

// passing the format followed by the date string you got
$gregorian1 = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
$gregorian2 = DateTime::createFromFormat('m-j-Y', '12-31-1999');
$gregorian3 = DateTime::createFromFormat('Y-m-d H:i:s', '2009-02-15 15:16:17');

$gregorian1 = new DateTime('next sunday');
$gregorian2 = new DateTime('yesterday');
$gregorian3 = new DateTime('1999-12-31');
$gregorian4 = new DateTime('2123-12-31 12:34:56');

$gregorian_bad = new DateTime('12-31-1999'); // this one probably fails

$gregorian = new DateTime('now');
$ethiopic = Andegna\DateTimeFactory::fromDateTime($gregorian);

// but we provided some shortcuts
$now1 = \Andegna\DateTimeFactory::now();
$now2 = new DateTime();

// if you wanna specify time zone
$sheger = new DateTimeZone('Africa/Addis_Ababa');
$now3 = \Andegna\DateTimeFactory::now($sheger);

// create some Ethiopian date how ever you want
$ethiopian_date = \Andegna\DateTimeFactory::now();

// you get a PHP DateTime object to play with
$gregorian = $now->toGregorian();

use Andegna\Converter\FromJdnConverter;

$jdn = 2457886;
$converter = new FromJdnConverter($jdn);

$day = $converter->getDay();     // 4
$month = $converter->getMonth(); // 9
$year = $converter->getYear();   // 2009

use Andegna\Converter\ToJdnConverter;

$converter = new ToJdnConverter(21,3,1986);
echo $jdn = $converter->getJdn();  // 2449322

$et = new Andegna\DateTime();

// ዓርብ, 04-ግን-2009 14:41:00 EAT
echo $et->format(DATE_COOKIE);

// create a Ethiopian Date `ToJdnConverter`
$converter = new Andegna\Converter\ToJdnConverter($et->getDay(), $et->getMonth(), $et->getYear());

// convert it to jdn
$jdn = $converter->getJdn();

// use the built-in php function to convert the jdn to the jewish calendar 
$jewish_date1 = jdtojewish($jdn);

// 9/16/5777
echo $jewish_date1;

// it also support formating for the return string
$jewish_date2 = jdtojewish($jdn, true,     CAL_JEWISH_ADD_ALAFIM_GERESH);

// convert the return string to utf-8
$jewish_date2 = iconv ('WINDOWS-1255', 'UTF-8', $jewish_date2); 

// טז אייר ה'תשעז
echo $jewish_date2;

$day = 29;
$month = 4;
$year = 2017;

// get the jdn using the built in php function
$jdn = juliantojd($month, $day, $year);

// convert the jd to Ethiopian Date
$converter = new Andegna\Converter\FromJdnConverter($jdn);

// create a `Andegna\DateTime` from the converted date
$ethiopic = Andegna\DateTimeFactory::fromConverter($converter);

// ዓርብ, 04-ግን-2009 00:00:00 EAT
echo $ethiopic->format(DATE_COOKIE);

// Friday, 12-May-2017 00:00:00 EAT
echo $ethiopic->toGregorian()->format(DATE_COOKIE);

// let's start from today
$today = new Andegna\DateTime();

// Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
$tomorrow = $today->add(new DateInterval('P1D'));
$after_some_days = $today->add(new DateInterval('P10DT4H'));
$after_6_hours = $today->add(new DateInterval('PT6H'));

// Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
$yesterday = $today->sub(new DateInterval('P1D'));
$before_some_days = $today->sub(new DateInterval('P10DT4H'));
$before_6_hours = $today->sub(new DateInterval('PT6H'));

// Alters the DateTime object
$tomorrow = $today->modify('+1 day');
$yesterday = $today->modify('-1 day');
$after_30_minutes = $today->modify('+30 minutes');
$after_30_minutes = $today->modify('next sunday');

$today = new Andegna\DateTime();
$tomorrow = (new Andegna\DateTime())->add(new DateInterval('P1DT2M3S'));

$diff = $today->diff($tomorrow); // returns a DateTimeInterval
var_dump($diff);

$now = new Andegna\DateTime();

// Let's play a game. It's called "Spot the difference"
echo $now->format(DATE_COOKIE);                   // ዓርብ, 04-ግን-2009 02:09:52 EAT
echo $now->toGregorian()->format(DATE_COOKIE);    // Friday, 12-May-2017 02:09:52 EAT

echo $now->format(DATE_ATOM);                      // 2009-09-04EAT02:09:52+03:00
echo $now->toGregorian()->format(DATE_ATOM);     // 2017-05-12T02:09:52+03:00

echo $now->format('F j ቀን Y');                    // ግንቦት 4 ቀን 2009
echo $now->toGregorian()->format('F j ቀን Y');     // May 12 ቀን 2017

echo $now->format('H:i:s A');                   // 10:09:17 ረፋድ
echo $now->toGregorian()->format('H:i:s A');    // 10:09:17 AM

$date = new Andegna\DateTime();

// ዓርብ፣ ግንቦት 04 ቀን 02:35:45 ውደቀት EAT 2009 ዓ/ም
echo $date->format(Andegna\Constants::DATE_ETHIOPIAN);

$date->modify('+8 hours');

// ዓርብ፣ ግንቦት 04 ቀን (ዮሐንስ) 10:35:45 ረፋድ EAT 2009 (ማርቆስ) ዓ/ም
echo $date->format(Andegna\Constants::DATE_ETHIOPIAN_ORTHODOX);

$date->modify('+1 year');

// ቅዳሜ፣ ግንቦት ፬ ቀን 10:35:45 ረፋድ EAT ፳፻፲ ዓ/ም
echo $date->format(Andegna\Constants::DATE_GEEZ);

$date->modify('-3 years')->modify('+1 day');

// ረቡዕ፣ ግንቦት ፭ ቀን (አቦ) 10:35:45 ረፋድ EAT ፳፻፯ (ዮሐንስ) ዓ/ም
echo $date->format(Andegna\Constants::DATE_GEEZ_ORTHODOX);

$easter = new Andegna\Holiday\Easter();

// እሑድ፣ ሚያዝያ 08 ቀን 00:00:00 እኩለ፡ሌሊት EAT 2009 ዓ/ም
echo $easter->get(2009)->format(Andegna\Constants::DATE_ETHIOPIAN).PHP_EOL;

// or just ...
foreach ([2006,2007,2008,2009,2010,2011,2012] as $year) {
    echo $easter->get($year)->format(Andegna\Constants::DATE_ETHIOPIAN).PHP_EOL;
}

use Andegna\Validator\DateValidator;

// true
$is_valid1 = (new DateValidator(15,9, 2009))->isValid();

// false
$is_valid2 = (new DateValidator(6,13, 2009))->isValid();

use Andegna\Validator\LeapYearValidator;

// false
$is_valid3 = (new LeapYearValidator(2009))->isValid();

// true
$is_valid4 = (new LeapYearValidator(2007))->isValid();