极品分享

php计算十二星座的函数代码

<?php 
/* 
* 计算星座的函数 string get_zodiac_sign(string month, string day) 
* 输入:月份,日期 
* 输出:星座名称或者错误信息 
*/ 

function get_zodiac_sign($month, $day) 
{ 
// 检查参数有效性 
if ($month < 1 || $month > 12 || $day < 1 || $day > 31) 
return (false); 
// 星座名称以及开始日期 
$signs = array( 
array( "20" => "宝瓶座"), 
array( "19" => "双鱼座"), 
array( "21" => "白羊座"), 
array( "20" => "金牛座"), 
array( "21" => "双子座"), 
array( "22" => "巨蟹座"), 
array( "23" => "狮子座"), 
array( "23" => "处女座"), 
array( "23" => "天秤座"), 
array( "24" => "天蝎座"), 
array( "22" => "射手座"), 
array( "22" => "摩羯座") 
); 
list($sign_start, $sign_name) = each($signs[(int)$month-1]); 
if ($day < $sign_start) 
list($sign_start, $sign_name) = each($signs[($month -2 < 0) ? $month = 11: $month -= 2]); 
return $sign_name; 
}//函数结束 
?>

php 出生生日取得星座

 <?php

function getConstellation($birthday, $format=null)
{
@header('Content-type: text/html;charset=UTF-8');
$pattern = '/^\d{4}-\d{1,2}-\d{1,2}$/';
if (!preg_match($pattern, $birthday, $matchs))
{
    return null;
}
$date = explode('-', $birthday);
$year = $date[0];
$month = $date[1];
$day   = $date[2];
if ($month <1 || $month>12 || $day < 1 || $day >31)
{
    return null;
}
//设定星座数组
$constellations = array(
      '摩羯座', '水瓶座', '双鱼座', '白羊座', '金牛座', '双子座',
      '巨蟹座','狮子座', '处女座', '天秤座', '天蝎座', '射手座',);

//设定星座结束日期的数组,用于判断
$enddays = array(19, 18, 20, 20, 20, 21, 22, 22, 22, 22, 21, 21,);
//如果参数format被设置,则返回值采用format提供的数组,否则使用默认的数组
if ($format != null)
{
    $values = $format;
}
else
{
    $values = $constellations;
}
//根据月份和日期判断星座
switch ($month)
{
    case 1:
      if ($day <= $enddays[0])
      {
        $constellation = $values[0];
      }
      else
      {
        $constellation = $values[1];
      }
      break;
    case 2:
      if ($day <= $enddays[1])
      {
        $constellation = $values[1];
      }
      else
      {
        $constellation = $values[2];
      }
      break;
    case 3:
      if ($day <= $enddays[2])
      {
        $constellation = $values[2];
      }
      else
      {
        $constellation = $values[3];
      }
      break;
    case 4:
      if ($day <= $enddays[3])
      {
        $constellation = $values[3];
      }
      else
      {
        $constellation = $values[4];
      }
      break;
    case 5:
      if ($day <= $enddays[4])
      {
        $constellation = $values[4];
      }
      else
      {
        $constellation = $values[5];
      }
      break;
    case 6:
      if ($day <= $enddays[5])
      {
        $constellation = $values[5];
      }
      else
      {
        $constellation = $values[6];
      }
      break;
    case 7:
      if ($day <= $enddays[6])
      {
        $constellation = $values[6];
      }
      else
      {
        $constellation = $values[7];
      }
      break;
    case 8:
      if ($day <= $enddays[7])
      {
        $constellation = $values[7];
      }
      else
      {
        $constellation = $values[8];
      }
      break;
    case 9:
      if ($day <= $enddays[8])
      {
        $constellation = $values[8];
      }
      else
      {
        $constellation = $values[9];
      }
      break;
    case 10:
      if ($day <= $enddays[9])
      {
        $constellation = $values[9];
      }
      else
      {
        $constellation = $values[10];
      }
      break;
    case 11:
      if ($day <= $enddays[10])
      {
        $constellation = $values[10];
      }
      else
      {
        $constellation = $values[11];
      }
      break;
    case 12:
      if ($day <= $enddays[11])
      {
        $constellation = $values[11];
      }
      else
      {
        $constellation = $values[0];
      }
      break;
}
return $constellation;
}


?>



php中根据生日判断星座、生肖程序代码

星座判断很简单我们要统计出每个星期所有日期时间段了,然后获取日期进行查询即可了,下面我给大家举两个实例,有需要的同学可参考。
星座:我是根据这个时间表写的,该时间表未必准确。
'水瓶座'=>'(1/22-2/21)',   '双鱼座'=>'(2/22-3/21)',
'白羊座'=>'(3/22-4/21)',   '金牛座'=>'(4/22-5/21)',
'双子座'=>'(5/22-6/21)',   '巨蟹座'=>'(6/22-7/21)',
'狮子座'=>'(7/22-8/21)',   '处女座'=>'(8/22-9/21)',
'天秤座'=>'(9/22-10/21)',  '天蝎座'=>'(10/22-11/21)',
'射手座'=>'(11/22-12/21)',  '摩羯座'=>'(12/22-1/21)'

根据日期判断星座函数
function yige_constellation($month, $day) {
 // 检查参数有效性 
 if ($month < 1 || $month > 12 || $day < 1 || $day > 31) return false;
 
 // 星座名称以及开始日期
 $constellations = array(
  array( "20" => "宝瓶座"),
  array( "19" => "双鱼座"),
  array( "21" => "白羊座"),
  array( "20" => "金牛座"),
  array( "21" => "双子座"),
  array( "22" => "巨蟹座"),
  array( "23" => "狮子座"),
  array( "23" => "处女座"),
  array( "23" => "天秤座"),
  array( "24" => "天蝎座"),
  array( "22" => "射手座"),
  array( "22" => "摩羯座")
 );
 
 list($constellation_start, $constellation_name) = each($constellations[(int)$month-1]);
 
 if ($day < $constellation_start) list($constellation_start, $constellation_name) = each($constellations[($month -2 < 0) ? $month = 11: $month -= 2]);
 
 return $constellation_name;
}

下面这个更全面可直接根据生日检查年龄,生肖,星座

<?php
/**
* 根据生日中的月份和日期来计算所属星座
*
* @param int $birth_month
* @param int $birth_date
* @return string
*/
function get_constellation($birth_month,$birth_date)
{
//判断的时候,为避免出现1和true的疑惑,或是判断语句始终为真的问题,这里统一处理成字符串形式
$birth_month = strval($birth_month);
 
$constellation_name = array(
      '水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座',
      '狮子座','处女座','天秤座','天蝎座)','射手座','摩羯座'
      );
 
if ($birth_date <= 22)
{
  if ('1' !== $birth_month)
  {
   $constellation = $constellation_name[$birth_month-2];
  }
  else
  {
   $constellation = $constellation_name[11];
  }
}
else
{
  $constellation = $constellation_name[$birth_month-1];
}
 
return $constellation;
}

/**
* 根据生日中的年份来计算所属生肖
*
* @param int $birth_year
* @return string
*/
function get_animal($birth_year)
{
//1900年是子鼠年
$animal = array(
    '子鼠','丑牛','寅虎','卯兔','辰龙','巳蛇',
    '午马','未羊','申猴','酉鸡','戌狗','亥猪'
    );
 
$my_animal = ($birth_year-1900)%12;
return $animal[$my_animal];
}

/**
* 根据生日来计算年龄
*
* 用Unix时间戳计算是最准确的,但不太好处理1970年之前出生的情况
* 而且还要考虑闰年的问题,所以就暂时放弃这种方式的开发,保留思想
*
* @param int $birth_year
* @param int $birth_month
* @param int $birth_date
* @return int
*/
function get_age($birth_year,$birth_month,$birth_date)
{
$now_age = 1; //实际年龄,以出生时为1岁计
$full_age = 0; //周岁,该变量放着,根据具体情况可以随时修改
 
$now_year   = date('Y',time());
$now_date_num  = date('z',time()); //该年份中的第几天
$birth_date_num = date('z',mktime(0,0,0,$birth_month,$birth_date,$birth_year));
 
$difference = $now_date_num - $birth_date_num;
if ($difference > 0)
{
  $full_age = $now_year - $birth_year;
}
else
{
  $full_age = $now_year - $birth_year - 1;
}
 
$now_age = $full_age + 1;
 
return $now_age;
}

?>


php 根据生日计算星座和生肖程序

一个php 根据生日计算星座和生肖程序,有需要的朋友可参考参考.
魔羯座(12/22 – 1/19)、水瓶座(1/20 – 2/18)、双鱼座(2/19 – 3/20)、牡羊座(3/21 – 4/20)、金牛座(4/21 – 5/20)、双子座(5/21 – 6/21)、巨蟹座(6/22 – 7/22)、狮子座(7/23 – 8/22)、处女座(8/23 – 9/22)、天秤座(9/23 – 10/22)、天蝎座(10/23 – 11/21)、射手座(11/22 – 12/21)
PHP实例代码如下:
/** 
*getConstellation 根据出生生日取得星座 
* 
*@param String $brithday 用于得到星座的日期 格式为yyyy-mm-dd 
* 
*@param Array $format 用于返回星座的名称 
* 
*@return String 
*/ 
function getConstellation($birthday, $format=null) 
{ 
$pattern = ‘/^d{4}-d{1,2}-d{1,2}$/’; 
if (!preg_match($pattern, $birthday, $matchs)) 
{ 
    return null; 
} 
$date = explode(‘-’, $birthday); 
$year = $date[0]; 
$month = $date[1]; 
$day   = $date[2]; 
if ($month <1 || $month>12 || $day < 1 || $day >31) 
{ 
    return null; 
} 
//设定星座数组 
$constellations = array( 
      ‘摩羯座’, ‘水瓶座’, ‘双鱼座’, ‘白羊座’, ‘金牛座’, ‘双子座’, 
      ‘巨蟹座’,'狮子座’, ‘处女座’, ‘天秤座’, ‘天蝎座’, ‘射手座’,); 
//或 ‍‍$constellations = array( 
      ‘Capricorn’, ‘Aquarius’, ‘Pisces’, ‘Aries’, ‘Taurus’, ‘Gemini’, 
      ‘Cancer’,'Leo’, ‘Virgo’, ‘Libra’, ‘Scorpio’, ‘Sagittarius’,); 
//设定星座结束日期的数组,用于判断 
$enddays = array(19, 18, 20, 20, 20, 21, 22, 22, 22, 22, 21, 21,); 
//如果参数format被设置,则返回值采用format提供的数组,否则使用默认的数组 
if ($format != null) 
{ 
    $values = $format; 
} 
else 
{ 
    $values = $constellations; 
} 
//根据月份和日期判断星座 
switch ($month) 
{ 
    case 1: 
      if ($day <= $enddays[0]) 
      { 
        $constellation = $values[0]; 
      } 
      else 
      { 
        $constellation = $values[1]; 
      } 
      break; 
    case 2: 
      if ($day <= $enddays[1]) 
      { 
        $constellation = $values[1]; 
      } 
      else 
      { 
        $constellation = $values[2]; 
      } 
      break; 
    case 3: 
      if ($day <= $enddays[2]) 
      { 
        $constellation = $values[2]; 
      } 
      else 
      { 
        $constellation = $values[3]; 
      } 
      break; 
    case 4: 
      if ($day <= $enddays[3]) 
      { 
        $constellation = $values[3]; 
      } 
      else 
      { 
        $constellation = $values[4]; 
      } 
      break; 
    case 5: 
      if ($day <= $enddays[4]) 
      { 
        $constellation = $values[4]; 
      } 
      else 
      { 
        $constellation = $values[5]; 
      } 
      break; 
    case 6: 
      if ($day <= $enddays[5]) 
      { 
        $constellation = $values[5]; 
      } 
      else 
      { 
        $constellation = $values[6]; 
      } 
      break; 
    case 7: 
      if ($day <= $enddays[6]) 
      { 
        $constellation = $values[6]; 
      } 
      else 
      { 
        $constellation = $values[7]; 
      } 
      break; 
    case 8: 
      if ($day <= $enddays[7]) 
      { 
        $constellation = $values[7]; 
      } 
      else 
      { 
        $constellation = $values[8]; 
      } 
      break; 
    case 9: 
      if ($day <= $enddays[8]) 
      { 
        $constellation = $values[8]; 
      } 
      else 
      { 
        $constellation = $values[9]; 
      } 
      break; 
    case 10: 
      if ($day <= $enddays[9]) 
      { 
        $constellation = $values[9]; 
      } 
      else 
      { 
        $constellation = $values[10]; 
      } 
      break; 
    case 11: 
      if ($day <= $enddays[10]) 
      { 
        $constellation = $values[10]; 
      } 
      else 
      { 
        $constellation = $values[11]; 
      } 
      break; 
    case 12: 
      if ($day <= $enddays[11]) 
      { 
        $constellation = $values[11]; 
      } 
      else 
      { 
        $constellation = $values[0]; 
      } 
      break; 
} 
return $constellation; 
} 
js格式的: 
根据生日的月份和日期,计算星座的js小函数(最简) 
// 根据生日的月份和日期,计算星座。  http://blog.111cn.net/cuixiping/ 
function getAstro(month,day){ 
    var s=”魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯”; 
    var arr=[20,19,21,21,21,22,23,23,23,23,22,22]; 
    return s.substr(month*2-(day<arr[month-1]?2:0),2); 
} 
// 取星座, 参数分别是 月份和日期 
function getxingzuo(month,day){ 
//by Go_Rush(阿舜) from http://ashun.cnblogs.com/ 
var d=new Date(1999,month-1,day,0,0,0); 
var arr=[]; 
arr.push(["魔羯座",new Date(1999, 0, 1,0,0,0)]) 
arr.push(["水瓶座",new Date(1999, 0,20,0,0,0)]) 
arr.push(["双鱼座",new Date(1999, 1,19,0,0,0)]) 
arr.push(["牡羊座",new Date(1999, 2,21,0,0,0)]) 
arr.push(["金牛座",new Date(1999, 3,21,0,0,0)]) 
arr.push(["双子座",new Date(1999, 4,21,0,0,0)]) 
arr.push(["巨蟹座",new Date(1999, 5,22,0,0,0)]) 
arr.push(["狮子座",new Date(1999, 6,23,0,0,0)]) 
arr.push(["处女座",new Date(1999, 7,23,0,0,0)]) 
arr.push(["天秤座",new Date(1999, 8,23,0,0,0)]) 
arr.push(["天蝎座",new Date(1999, 9,23,0,0,0)]) 
arr.push(["射手座",new Date(1999,10,22,0,0,0)]) 
arr.push(["魔羯座",new Date(1999,11,22,0,0,0)]) 
for(var i=arr.length-1;i>=0;i–){ 
if (d>=arr[i][1]) return arr[i][0]; 
} 
} 
function getxingzuo(month,day){ 
var s=”魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯”; 
var arr=[19,50,84,116,148,181,214,246,278,310,341,373,383]; 
for(var i=0;i<arr.length;i++){ 
if ((((month-1)<<5)+day) <= arr[i]) return s.substr(i*2,2); 
} 
return “error”; 
} 
计算生肖的: 
function birthday2BornTag($birthday){ 
$year = substr($birthday,0,4); 
$bornTagarray = array(“猴”, “鸡”, “狗”, “猪”, “鼠”, “牛”, “虎”, “兔”, “龙”, “蛇”, 
 
“马”, “羊”); 
$index = $year%12; 
$bornTag = $bornTagarray[$index]; 
return $bornTag; 
} 
echo birthday2BornTag(’1983-12-19′);

获取生日对应星座的PHP函数

$birth = getParam('birth', 'POST');
	
	$str = substr($birth,0,4);//截取年
	$month = substr($birth,5,2);//截取月
	$day = substr($birth,8,2);//截取日

	$year = date('Y',time());//转换年
	$age = $year - $str;
	
	/**
     * 获取指定日期对应星座
     * 
     * @param integer $month 月份 1-12
     * @param integer $day 日期 1-31
     * @return boolean|string
     */
	function getConstellation($month, $day)
    {
        $day   = intval($day);
        $month = intval($month);
        if ($month < 1 || $month > 12 || $day < 1 || $day > 31) return false;
        $signs = array(
                array('20'=>'宝瓶座'),
                array('19'=>'双鱼座'),
                array('21'=>'白羊座'),
                array('20'=>'金牛座'),
                array('21'=>'双子座'),
                array('22'=>'巨蟹座'),
                array('23'=>'狮子座'),
                array('23'=>'处女座'),
                array('23'=>'天秤座'),
                array('24'=>'天蝎座'),
                array('22'=>'射手座'),
                array('22'=>'摩羯座')
        );
        list($start, $name) = each($signs[$month-1]);
        if ($day < $start)
            list($start, $name) = each($signs[($month-2 < 0) ? 11 : $month-2]);
        return $name;
    }

	$constellation = getConstellation($month, $day);

php计算十二星座的函数代码

<?php 
/* 
* 计算星座的函数 string get_zodiac_sign(string month, string day) 
* 输入:月份,日期 
* 输出:星座名称或者错误信息 
*/ 

function get_zodiac_sign($month, $day) 
{ 
// 检查参数有效性 
if ($month < 1 || $month > 12 || $day < 1 || $day > 31) 
return (false); 
// 星座名称以及开始日期 
$signs = array( 
array( "20" => "宝瓶座"), 
array( "19" => "双鱼座"), 
array( "21" => "白羊座"), 
array( "20" => "金牛座"), 
array( "21" => "双子座"), 
array( "22" => "巨蟹座"), 
array( "23" => "狮子座"), 
array( "23" => "处女座"), 
array( "23" => "天秤座"), 
array( "24" => "天蝎座"), 
array( "22" => "射手座"), 
array( "22" => "摩羯座") 
); 
list($sign_start, $sign_name) = each($signs[(int)$month-1]); 
if ($day < $sign_start) 
list($sign_start, $sign_name) = each($signs[($month -2 < 0) ? $month = 11: $month -= 2]); 
return $sign_name; 
}//函数结束 
?>


php下实现农历日历的代码

php农历日历 

<? 
  ########################################### 
  #作者: 沈潋(S&S Lab)                      # 
  #E-mail:shenlian@hotmail.com              # 
  #web: http://www.focus-2000.com           # 
  #                                         # 
  #版权声明:                                # 
  #作者开放版权                             # 
  #允许无条件地使用本文件的全部或部分       # 
  #可作任何修改或增补                       # 
  #如果您使用了本文件,请给作者写一封E-mail # 
  #如有问题欢迎同作者联系                   # 
  #谢谢您的关注!                            # 
  ########################################### 

  /* 
   *修改:小许 
   *mail: san@xfocus.org 
   *web : http://www.xfocus.org 
   * 
   *说实在的,我对古历可以说一窍不通,下面这个数组还是不理解 可是作者的信箱好像不通 
   *基本上是依样画葫芦,所以出现错误,见笑见笑,呵呵 
   */ 

  #农历每月的天数 
  $everymonth=array( 
                    0=>array(8,0,0,0,0,0,0,0,0,0,0,0,29,30,7,1), 
                    1=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,8,2), 
                    2=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,9,3), 
                    3=>array(5,29,30,29,30,29,29,30,29,29,30,30,29,30,10,4), 
                    4=>array(0,30,30,29,30,29,29,30,29,29,30,30,29,0,1,5), 
                    5=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,2,6), 
                    6=>array(4,29,30,30,29,30,29,30,29,30,29,30,29,30,3,7), 
                    7=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,4,8), 
                    8=>array(0,30,29,29,30,30,29,30,29,30,30,29,30,0,5,9), 
                    9=>array(2,29,30,29,29,30,29,30,29,30,30,30,29,30,6,10), 
                    10=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,7,11), 
                    11=>array(6,30,29,30,29,29,30,29,29,30,30,29,30,30,8,12), 
                    12=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,9,1), 
                    13=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,10,2), 
                    14=>array(5,30,30,29,30,29,30,29,30,29,30,29,29,30,1,3), 
                    15=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,2,4), 
                    16=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,3,5), 
                    17=>array(2,30,29,29,30,29,30,30,29,30,30,29,30,29,4,6), 
                    18=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,5,7), 
                    19=>array(7,29,30,29,29,30,29,29,30,30,29,30,30,30,6,8), 
                    20=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,7,9), 
                    21=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,8,10), 
                    22=>array(5,30,29,30,30,29,29,30,29,29,30,29,30,30,9,11), 
                    23=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,10,12), 
                    24=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,1,1), 
                    25=>array(4,30,29,30,29,30,30,29,30,30,29,30,29,30,2,2), 
                    26=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,3,3), 
                    27=>array(0,30,29,29,30,29,30,29,30,29,30,30,30,0,4,4), 
                    28=>array(2,29,30,29,29,30,29,29,30,29,30,30,30,30,5,5), 
                    29=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,6,6), 
                    30=>array(6,29,30,30,29,29,30,29,29,30,29,30,30,29,7,7), 
                    31=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,8,8), 
                    32=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,9,9), 
                    33=>array(5,29,30,30,29,30,30,29,30,29,30,29,29,30,10,10), 
                    34=>array(0,29,30,29,30,30,29,30,29,30,30,29,30,0,1,11), 
                    35=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,2,12), 
                    36=>array(3,30,29,29,30,29,29,30,30,29,30,30,30,29,3,1), 
                    37=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,4,2), 
                    38=>array(7,30,30,29,29,30,29,29,30,29,30,30,29,30,5,3), 
                    39=>array(0,30,30,29,29,30,29,29,30,29,30,29,30,0,6,4), 
                    40=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,7,5), 
                    41=>array(6,30,30,29,30,30,29,30,29,29,30,29,30,29,8,6), 
                    42=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,9,7), 
                    43=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,10,8), 
                    44=>array(4,30,29,30,29,30,29,30,29,30,30,29,30,30,1,9), 
                    45=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,2,10), 
                    46=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,3,11), 
                    47=>array(2,30,30,29,29,30,29,29,30,29,30,29,30,30,4,12), 
                    48=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,5,1), 
                    49=>array(7,30,29,30,30,29,30,29,29,30,29,30,29,30,6,2), 
                    50=>array(0,29,30,30,29,30,30,29,29,30,29,30,29,0,7,3), 
                    51=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,8,4), 
                    52=>array(5,29,30,29,30,29,30,29,30,30,29,30,29,30,9,5), 
                    53=>array(0,29,30,29,29,30,30,29,30,30,29,30,29,0,10,6), 
                    54=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,1,7), 
                    55=>array(3,29,30,29,30,29,29,30,29,30,29,30,30,30,2,8), 
                    56=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,3,9), 
                    57=>array(8,30,29,30,29,30,29,29,30,29,30,29,30,29,4,10), 
                    58=>array(0,30,30,30,29,30,29,29,30,29,30,29,30,0,5,11), 
                    59=>array(0,29,30,30,29,30,29,30,29,30,29,30,29,0,6,12), 
                    60=>array(6,30,29,30,29,30,30,29,30,29,30,29,30,29,7,1), 
                    61=>array(0,30,29,30,29,30,29,30,30,29,30,29,30,0,8,2), 
                    62=>array(0,29,30,29,29,30,29,30,30,29,30,30,29,0,9,3), 
                    63=>array(4,30,29,30,29,29,30,29,30,29,30,30,30,29,10,4), 
                    64=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,1,5), 
                    65=>array(0,29,30,29,30,29,29,30,29,29,30,30,29,0,2,6), 
                    66=>array(3,30,30,30,29,30,29,29,30,29,29,30,30,29,3,7), 
                    67=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,4,8), 
                    68=>array(7,29,30,29,30,30,29,30,29,30,29,30,29,30,5,9), 
                    69=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,6,10), 
                    70=>array(0,30,29,29,30,29,30,30,29,30,30,29,30,0,7,11), 
                    71=>array(5,29,30,29,29,30,29,30,29,30,30,30,29,30,8,12), 
                    72=>array(0,29,30,29,29,30,29,30,29,30,30,29,30,0,9,1), 
                    73=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,10,2), 
                    74=>array(4,30,30,29,30,29,29,30,29,29,30,30,29,30,1,3), 
                    75=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,2,4), 
                    76=>array(8,30,30,29,30,29,30,29,30,29,29,30,29,30,3,5), 
                    77=>array(0,30,29,30,30,29,30,29,30,29,30,29,29,0,4,6), 
                    78=>array(0,30,29,30,30,29,30,30,29,30,29,30,29,0,5,7), 
                    79=>array(6,30,29,29,30,29,30,30,29,30,30,29,30,29,6,8), 
                    80=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,7,9), 
                    81=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,8,10), 
                    82=>array(4,30,29,30,29,29,30,29,29,30,29,30,30,30,9,11), 
                    83=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,10,12), 
                    84=>array(10,30,29,30,30,29,29,30,29,29,30,29,30,30,1,1), 
                    85=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,2,2), 
                    86=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,3,3), 
                    87=>array(6,30,29,30,29,30,30,29,30,30,29,30,29,29,4,4), 
                    88=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,5,5), 
                    89=>array(0,30,29,29,30,29,29,30,30,29,30,30,30,0,6,6), 
                    90=>array(5,29,30,29,29,30,29,29,30,29,30,30,30,30,7,7), 
                    91=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,8,8), 
                    92=>array(0,29,30,30,29,29,30,29,29,30,29,30,30,0,9,9), 
                    93=>array(3,29,30,30,29,30,29,30,29,29,30,29,30,29,10,10), 
                    94=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,1,11), 
                    95=>array(8,29,30,30,29,30,29,30,30,29,29,30,29,30,2,12), 
                    96=>array(0,29,30,29,30,30,29,30,29,30,30,29,29,0,3,1), 
                    97=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,4,2), 
                    98=>array(5,30,29,29,30,29,29,30,30,29,30,30,29,30,5,3), 
                    99=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,6,4), 
                    100=>array(0,30,30,29,29,30,29,29,30,29,30,30,29,0,7,5), 
                    101=>array(4,30,30,29,30,29,30,29,29,30,29,30,29,30,8,6), 
                    102=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,9,7), 
                    103=>array(0,30,30,29,30,30,29,30,29,29,30,29,30,0,10,8), 
                    104=>array(2,29,30,29,30,30,29,30,29,30,29,30,29,30,1,9), 
                    105=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,2,10), 
                    106=>array(7,30,29,30,29,30,29,30,29,30,30,29,30,30,3,11), 
                    107=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,4,12), 
                    108=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,5,1), 
                    109=>array(5,30,30,29,29,30,29,29,30,29,30,29,30,30,6,2), 
                    110=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,7,3), 
                    111=>array(0,30,29,30,30,29,30,29,29,30,29,30,29,0,8,4), 
                    112=>array(4,30,29,30,30,29,30,29,30,29,30,29,30,29,9,5), 
                    113=>array(0,30,29,30,29,30,30,29,30,29,30,29,30,0,10,6), 
                    114=>array(9,29,30,29,30,29,30,29,30,30,29,30,29,30,1,7), 
                    115=>array(0,29,30,29,29,30,29,30,30,30,29,30,29,0,2,8), 
                    116=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,3,9), 
                    117=>array(6,29,30,29,30,29,29,30,29,30,29,30,30,30,4,10), 
                    118=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,5,11), 
                    119=>array(0,30,29,30,29,30,29,29,30,29,29,30,30,0,6,12), 
                    120=>array(4,29,30,30,30,29,30,29,29,30,29,30,29,30,7,1) 
                   ); 
############################## 
  #农历天干 
  $mten=array("null","甲","乙","丙","丁","戊","己","庚","辛","壬","癸"); 
  #农历地支 
  $mtwelve=array("null","子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)", 
                 "巳(蛇)","午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)"); 
  #农历月份 
  $mmonth=array("闰","正","二","三","四","五","六", 
                "七","八","九","十","十一","十二","月"); 
  #农历日 
  $mday=array("null","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十", 
              "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十", 
              "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"); 
############################## 
  #星期 
  $weekday = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); 

  #阳历总天数 至1900年12月21日 
  $total=11; 
  #阴历总天数 
  $mtotal=0; 
############################## 
  #获得当日日期 
  $today=getdate(); 
  if($today["year"]<1901 || $today["year"]>2020) die("年份出错!"); 

  $cur_wday=$today["wday"]; 

  for($y=1901;$y<$today["year"];$y++) { //计算到所求日期阳历的总天数-自1900年12月21日始,先算年的和 
       $total+=365; 
       if ($y%4==0) $total++; 
  } 

  switch($today["mon"]) { //再加当年的几个月 
         case 12: 
              $total+=30; 
         case 11: 
              $total+=31; 
         case 10: 
              $total+=30; 
         case 9: 
              $total+=31; 
         case 8: 
              $total+=31; 
         case 7: 
              $total+=30; 
         case 6: 
              $total+=31; 
         case 5: 
              $total+=30; 
         case 4: 
              $total+=31; 
         case 3: 
              $total+=28; 
         case 2: 
              $total+=31; 
  } 

  if($today["year"]%4 == 0 && $today["mon"]>2) $total++; //如果当年是闰年还要加一天 

  $total=$total+$today["mday"]-1; //加当月的天数 

  $flag1=0;  //判断跳出循环的条件 
  $j=0; 
  while ($j<=120){  //用农历的天数累加来判断是否超过阳历的天数 
      $i=1; 
      while ($i<=13){ 
            $mtotal+=$everymonth[$j][$i]; 
            if ($mtotal>=$total){ 
                 $flag1=1; 
                 break; 
            } 
            $i++; 
      } 
      if ($flag1==1) break; 
      $j++; 
  } 

  if($everymonth[$j][0]<>0 and $everymonth[$j][0]<$i){ //原来错在这里,对闰月没有修补 
      $mm=$i-1; 
  } 
  else{ 
      $mm=$i; 
  } 

  if($i==$everymonth[$j][0]+1 and $everymonth[$j][0]<>0) { 
      $nlmon=$mmonth[0].$mmonth[$mm];#闰月 
  } 
  else { 
      $nlmon=$mmonth[$mm].$mmonth[13]; 
  } 

  #计算所求月份1号的农历日期 
  $md=$everymonth[$j][$i]-($mtotal-$total); 
  if($md > $everymonth[$j][$i]) 
      $md-=$everymonth[$j][$i]; 
  $nlday=$mday[$md]; 

  $nowday=date("Y年n月j日 ").$weekday[$cur_wday]."<br>".$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]]."年".$nlmon.$nlday; 
  echo "<font color=#3333ff>$nowday</font>"; 
?>

PHP农历公历转换

<?php
/*
    云南省曲靖师范学院计算机科学与工程学院-杨海熙编写
    2009-9-3
*/ 
class Lunar
{
    private  $_SMDay = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);//定义公历月分天数
    private  $_LStart = 1950 ;//农历从1950年开始
    private  $_LMDay = array(
    //差:该年的农历正月初一到该年公历1月1日的天数;1~12:农历月份天数;闰:如有闰月,记录该月平月天数
    //    差  1  2  3  4  5  6  7  8  9 10 11 12 闰
    array(47,29,30,30,29,30,30,29,29,30,29,30,29),
    array(36,30,29,30,30,29,30,29,30,29,30,29,30),
    array(6,29,30,29,30,59,29,30,30,29,30,29,30,29),    //五月29 闰五月30
    array(44,29,30,29,29,30,30,29,30,30,29,30,29),
    array(33,30,29,30,29,29,30,29,30,30,29,30,30),
    array(23,29,30,59,29,29,30,29,30,29,30,30,30,29),    //三月29 闰三月30
    array(42,29,30,29,30,29,29,30,29,30,29,30,30),
    array(30,30,29,30,29,30,29,29,59,30,29,30,29,30),    //八月30 闰八月29
    array(48,30,30,30,29,30,29,29,30,29,30,29,30),
    array(38,29,30,30,29,30,29,30,29,30,29,30,29),
    array(27,30,29,30,29,30,59,30,29,30,29,30,29,30),    //六月30 闰六月29
    array(45,30,29,30,29,30,29,30,30,29,30,29,30),
    array(35,29,30,29,29,30,29,30,30,29,30,30,29),
    array(24,30,29,30,58,30,29,30,29,30,30,30,29,29),    //四月29 闰四月29
    array(43,30,29,30,29,29,30,29,30,29,30,30,30),
    array(32,29,30,29,30,29,29,30,29,29,30,30,29),
    array(20,30,30,59,30,29,29,30,29,29,30,30,29,30),    //三月30 闰三月29
    array(39,30,30,29,30,30,29,29,30,29,30,29,30),
    array(29,29,30,29,30,30,29,59,30,29,30,29,30,30),    //七月30 闰七月29
    array(47,29,30,29,30,29,30,30,29,30,29,30,29),
    array(36,30,29,29,30,29,30,30,29,30,30,29,30),
    array(26,29,30,29,29,59,30,29,30,30,30,29,30,30),    //五月30 闰五月29
    array(45,29,30,29,29,30,29,30,29,30,30,29,30),
    array(33,30,29,30,29,29,30,29,29,30,30,29,30),
    array(22,30,30,29,59,29,30,29,29,30,30,29,30,30),    //四月30 闰四月29
    array(41,30,30,29,30,29,29,30,29,29,30,29,30),
    array(30,30,30,29,30,29,30,29,59,29,30,29,30,30),    //八月30 闰八月29
    array(48,30,29,30,30,29,30,29,30,29,30,29,29),
    array(37,30,29,30,30,29,30,30,29,30,29,30,29),
    array(27,30,29,29,30,29,60,29,30,30,29,30,29,30),    //六月30 闰六月30
    array(46,30,29,29,30,29,30,29,30,30,29,30,30),
    array(35,29,30,29,29,30,29,29,30,30,29,30,30),
    array(24,30,29,30,58,30,29,29,30,29,30,30,30,29),    //四月29 闰四月29
    array(43,30,29,30,29,29,30,29,29,30,29,30,30),
    array(32,30,29,30,30,29,29,30,29,29,59,30,30,30),    //十月30 闰十月29
    array(50,29,30,30,29,30,29,30,29,29,30,29,30),
    array(39,29,30,30,29,30,30,29,30,29,30,29,29),
    array(28,30,29,30,29,30,59,30,30,29,30,29,29,30),    //六月30 闰六月29
    array(47,30,29,30,29,30,29,30,30,29,30,30,29),
    array(36,30,29,29,30,29,30,29,30,29,30,30,30),
    array(26,29,30,29,29,59,29,30,29,30,30,30,30,30),    //五月30 闰五月29
    array(45,29,30,29,29,30,29,29,30,29,30,30,30),
    array(34,29,30,30,29,29,30,29,29,30,29,30,30),
    array(22,29,30,59,30,29,30,29,29,30,29,30,29,30),    //三月30 闰三月29
    array(40,30,30,30,29,30,29,30,29,29,30,29,30),
    array(30,29,30,30,29,30,29,30,59,29,30,29,30,30),    //八月30 闰八月29
    array(49,29,30,29,30,30,29,30,29,30,30,29,29),
    array(37,30,29,30,29,30,29,30,30,29,30,30,29),
    array(27,30,29,29,30,58,30,30,29,30,30,29,30,29),    //五月29 闰五月29
    array(46,30,29,29,30,29,29,30,29,30,30,30,29),
    array(35,30,30,29,29,30,29,29,30,29,30,30,29),
    array(23,30,30,29,59,30,29,29,30,29,30,29,30,30),    //四月30 闰四月29
    array(42,30,30,29,30,29,30,29,29,30,29,30,29),
    array(31,30,30,29,30,30,29,30,29,29,30,29,30),
    array(21,29,59,30,30,29,30,29,30,29,30,29,30,30),    //二月30 闰二月29
    array(39,29,30,29,30,29,30,30,29,30,29,30,29),
    array(28,30,29,30,29,30,29,59,30,30,29,30,30,30),    //七月30 闰七月29
    array(48,29,29,30,29,29,30,29,30,30,30,29,30),
    array(37,30,29,29,30,29,29,30,29,30,30,29,30),
    array(25,30,30,29,29,59,29,30,29,30,29,30,30,30),    //五月30 闰五月29
    array(44,30,29,30,29,30,29,29,30,29,30,29,30),
    array(33,30,29,30,30,29,30,29,29,30,29,30,29),
    array(22,30,29,30,59,30,29,30,29,30,29,30,29,30),    //四月30 闰四月29
    array(40,30,29,30,29,30,30,29,30,29,30,29,30),
    array(30,29,30,29,30,29,30,29,30,59,30,29,30,30),    //九月30 闰九月29
    array(49,29,30,29,29,30,29,30,30,30,29,30,29),
    array(38,30,29,30,29,29,30,29,30,30,29,30,30),
    array(27,29,30,29,30,29,59,29,30,29,30,30,30,29),    //六月29 闰六月30
    array(46,29,30,29,30,29,29,30,29,30,29,30,30),
    array(35,30,29,30,29,30,29,29,30,29,29,30,30),
    array(24,29,30,30,59,30,29,29,30,29,30,29,30,30),    //四月30 闰四月29
    array(42,29,30,30,29,30,29,30,29,30,29,30,29),
    array(31,30,29,30,29,30,30,29,30,29,30,29,30),
    array(21,29,59,29,30,30,29,30,30,29,30,29,30,30),    //二月30 闰二月29
    array(40,29,30,29,29,30,29,30,30,29,30,30,29),
    array(28,30,29,30,29,29,59,30,29,30,30,30,29,30),    //六月30 闰六月29
    array(47,30,29,30,29,29,30,29,29,30,30,30,29),
    array(36,30,30,29,30,29,29,30,29,29,30,30,29),
    array(25,30,30,30,29,59,29,30,29,29,30,30,29,30),    //五月30 闰五月29
    array(43,30,30,29,30,29,30,29,30,29,29,30,30),
    array(33,29,30,29,30,30,29,30,29,30,29,30,29),
    array(22,29,30,59,30,29,30,30,29,30,29,30,29,30),    //三月30 闰三月29
    array(41,30,29,29,30,29,30,30,29,30,30,29,30),
    array(30,29,30,29,29,30,29,30,29,30,30,59,30,30),    //十一月30 闰十一月29
    array(49,29,30,29,29,30,29,30,29,30,30,29,30),
    array(38,30,29,30,29,29,30,29,29,30,30,29,30),
    array(27,30,30,29,30,29,59,29,29,30,29,30,30,29),    //六月29 闰六月30
    array(45,30,30,29,30,29,29,30,29,29,30,29,30),
    array(34,30,30,29,30,29,30,29,30,29,29,30,29),
    array(23,30,30,29,30,59,30,29,30,29,30,29,29,30),    //五月30 闰五月29
    array(42,30,29,30,30,29,30,29,30,30,29,30,29),
    array(31,29,30,29,30,29,30,30,29,30,30,29,30),
    array(21,29,59,29,30,29,30,29,30,30,29,30,30,30),    //二月30 闰二月29
    array(40,29,30,29,29,30,29,29,30,30,29,30,30),
    array(29,30,29,30,29,29,30,58,30,29,30,30,30,29),    //七月29 闰七月29
    array(47,30,29,30,29,29,30,29,29,30,29,30,30),
    array(36,30,29,30,29,30,29,30,29,29,30,29,30),
    array(25,30,29,30,30,59,29,30,29,29,30,29,30,29),    //五月29 闰五月30
    array(44,29,30,30,29,30,30,29,30,29,29,30,29),
    array(32,30,29,30,29,30,30,29,30,30,29,30,29),
    array(22,29,30,59,29,30,29,30,30,29,30,30,29,29),    //三月29 闰三月30       
    );
    //是否闰年
    private function IsLeapYear($AYear){
        return ($AYear % 4 == 0) && (($AYear % 100 != 0) || ($AYear % 400 == 0));
    }
    //公历该月的天数(year:年份; month:月份)
    private function GetSMon($year,$month)
    {
        if($this->IsLeapYear($year) && $month == 2)
            return 29;
        else
            return $this->_SMDay[$month];
    }
    //农历名称转换
    private function LYearName($year)
    {
        $Name = array("零","一","二","三","四","五","六","七","八","九");
        for($i=0;$i<4;$i++)
            for($k=0;$k<10;$k++)
                if($year[$i]==$k)
                    $tmp.=$Name[$k];
        return $tmp;
    }
     
    private function LMonName($month)
    {
        if($month >=1 && $month <=12 )
        {
            $Name = array( 1=>"正","二","三","四","五","六","七","八","九","十","十一","十二");
            return $Name[$month];
        }
        return $month;
    }
     
    private function LDayName($day)
    {
        if($day >=1 && $day <=30 )
        {
            $Name = array( 1 =>
            "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
            "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
            "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"
            );
            return $Name[$day];
        } 
        return $day;
    }
     
    //公历转农历(Sdate:公历日期)
    public function S2L($date)
    {
        list($year, $month, $day) = explode("-", $date);
        if($year <= 1951 || $month <= 0 || $day <= 0 || $year >= 2051 ) return false;
        //获取查询日期到当年1月1日的天数
        $date1 = strtotime($year."-01-01");//当年1月1日
        $date2 = strtotime($year."-".$month."-".$day);
        $days=round(($date2-$date1)/3600/24);
        $days += 1;
        //获取相应年度农历数据,化成数组Larray
        $Larray = $this->_LMDay[$year - $this->_LStart];
        if($days <= $Larray[0])
        {
            $Lyear = $year - 1;
            $days = $Larray[0] - $days;
            $Larray = $this->_LMDay[$Lyear - $this->_LStart];
            if($days < $Larray[12])
            {
                $Lmonth = 12;
                $Lday = $Larray[12] - $days;
            }
            else
            {
                $Lmonth = 11;
                $days = $days - $Larray[12];
                $Lday = $Larray[11] - $days;
            }           
        }
        else
        {
            $Lyear = $year;
            $days = $days - $Larray[0];
            for($i = 1;$i <= 12;$i++)
            {
                if($days > $Larray[$i]) $days = $days - $Larray[$i];
                else
                {
                    if ($days > 30){
                        $days = $days - $Larray[13];
                        $Ltype = 1;
                    }
                 
                    $Lmonth = $i;
                    $Lday = $days;
                    break;
                }
            }
        }
        return mktime(0, 0, 0, $Lmonth, $Lday, $Lyear);
        //$Ldate = $Lyear."-".$Lmonth."-".$Lday;
        //$Ldate = $this->LYearName($Lyear)."年".$this->LMonName($Lmonth)."月".$this->LDayName($Lday);
        //if($Ltype) $Ldate.="(闰)";
        //return $Ldate;
    }
    //农历转公历(date:农历日期; type:是否闰月)
    public function L2S($date,$type = 0)
    {
        list($year, $month, $day) = split("-",$date);
        if($year <= 1951 || $month <= 0 || $day <= 0 || $year >= 2051 ) return false;
        $Larray = $this->_LMDay[$year - $this->_LStart];
        if($type == 1 && count($Larray)<=12 ) return false;//要求查询闰,但查无闰月
        //如果查询的农历是闰月并该年度农历数组存在闰月数据就获取
        if($Larray[$month]>30 && $type == 1 && count($Larray) >=13)   $day = $Larray[13] + $day;
        //获取该年农历日期到公历1月1日的天数
        $days = $day;
        for($i=0;$i<=$month-1;$i++)
            $days += $Larray[$i];
        //当查询农历日期距离公历1月1日超过一年时
        if($days > 366 || ($this->GetSMon($month,2)!=29 && $days>365 ))
        {
            $Syear = $year +1;
            if($this->GetSMon($month,2)!=29) 
                $days-=366;
            else
                $days-=365;
            if($days > $this->_SMDay[1]) 
            {
                $Smonth = 2;
                $Sday = $days - $this->_SMDay[1];
            }
            else
            {
                $Smonth = 1;
                $Sday = $days;
            }       
        }
        else
        {
            $Syear =$year;
            for($i=1;$i<=12;$i++)
            {
                if($days > $this->GetSMon($Syear,$i))
                    $days-=$this->GetSMon($Syear,$i);
                else
                {
                    $Smonth = $i;
                    $Sday = $days;
                    break;
                }
            }
        }
        return mktime(0, 0, 0, $Smonth, $Sday, $Syear);
        //$Sdate = $Syear."-".$Smonth."-".$Sday;
        //return $Sdate;
    }
}
?>
<?php
    require_once 'Lunar.php';
    $today = date("Y-m-d");
    $lunar = new Lunar();
    //公历转农历
    $nl = date("Y-m-d",$lunar->S2L($today));
    //农历转公历
    $gl = date("Y-m-d",$lunar->L2S($nl));    
    echo "今天公历是:$today<br/>";
    echo "转为农历是:$nl<br/>";
    echo "转回公历是:$gl<br/>";
?>


2015-06-15 0 /
PHP学习
/
标签: 

评论回复

回到顶部