Coder Social home page Coder Social logo

ionepub / lunar Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 7 KB

A Chinese calendar transform class 农历公历转换类,农历公历转换,阳历转阴历,阴历转阳历,今天的阴历日期

PHP 100.00%
calendar lunar solar solar2lunar lunar2solar

lunar's Introduction

lunar

农历公历转换,阳历转阴历,阴历转阳历,今天的阴历日期。 A Chinese calendar transform class.

Latest Stable Version

Total Downloads

Latest Unstable Version

License

安装

使用composer安装(建议)

# 稳定版本
composer require ionepub/lunar
composer require --prefer-dist ionepub/lunar

# 开发版本
composer require ionepub/lunar:dev-master -vvv

直接下载

下载地址:https://github.com/ionepub/lunar/releases

使用

实例化

require 'vendor/autoload.php';
use Ionepub\Lunar;

$lunar = Lunar::getInstance();

设置一个阳历日期

$lunar->solar(2018, 9, 10);

# 注意,不能这样传入日期,这样的参数可能会被过滤,如果是两位的日期,应转为字符串
$lunar->solar(2018, 09, 08); // success
$lunar->solar('2018', '09', '08'); // failed

设置一个阴历日期

$lunar->lunar(2018, 8, 5);
$lunar->lunar('2018', '08', '05');

// lunar方法中第四个参数表示阴历闰月,如果当前阴历日期是闰月的,需要明确传递true
$lunar->lunar(2017, 6, 2, true);

设置日期为今天

$lunar->solar();
$lunar->lunar();

获取日期信息

$result = $lunar->get();
// =>
//    Array
//    (
//	    [year] => 阳历年
//	    [month] => 阳历月
//	    [day] => 阳历日
//	    [lunar_year] => 阴历年
//	    [lunar_month] => 阴历月
//	    [lunar_day] => 阴历日
//	    [lunar_year_chinese] => (中文)阴历年
//	    [lunar_month_chinese] => (中文)阴历月
//	    [lunar_day_chinese] => (中文)阴历日
//	    [is_leap] => 是否闰月
//    )

支持链式操作:

$result = $lunar->solar()->get();

获取日期字符串

通过向str()方法传递第一个参数,可以指定返回的日期类型,分别有:

  • Lunar::SOLAR 阳历日期,默认
  • Lunar::LUNAR 阴历日期
  • Lunar::LUNAR_CN 阴历日期(中文)
$date_str = $lunar->str(); // 2018-09-14
$date_str = $lunar->str(Lunar::SOLAR); // 2018-09-14
$date_str = $lunar->str(Lunar::LUNAR); // 2018-08-05
$date_str = $lunar->str(Lunar::LUNAR_CN); // 二零一八-八月-初五

可以通过向str()方法传递分隔符参数来设置返回的字符串格式,默认 -

$date_str = $lunar->str(Lunar::SOLAR); // 2018-09-14
$date_str = $lunar->str(Lunar::SOLAR, ' '); // 2018 09 14
$date_str = $lunar->str(Lunar::SOLAR, '.'); // 2018.09.14

同样支持链式操作:

$date_str = $lunar->lunar()->str();

示例

#已知用户的生日,获取下一次生日的日期

# 如果为阳历生日
$date = '1993-09-14';
$date_arr = explode("-", $date);
$date_arr[0] = date('Y');
if( date("Y-m-d") > implode("-", $date_arr) ){
    // 今年生日已过,获取下一年
    $date_arr[0]++;
}
$next = $calendar->solar($date_arr[0], $date_arr[1], $date_arr[2])->get();
print_r($next);

# 如果为阴历生日
$date = '1993-09-14';
$date_arr = explode("-", $date);
$date_arr[0] = date('Y');
if( $calendar->lunar()->str() > implode("-", $date_arr) ){
    // 今年生日已过,获取下一年
    $date_arr[0]++;
}
$next = $calendar->lunar($date_arr[0], $date_arr[1], $date_arr[2])->get();
print_r($next);

lunar's People

Contributors

ionepub avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.