php专区

 首页 > php专区 > PHP高级 > 日期 > php中根据生日判断星座、生肖程序代码 - php日期

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

分享到:
【字体:
导读:
          星座判断很简单我们要统计出每个星期所有日期时间段了,然后获取日期进行查询即可了,下面我给大家举两个实例,有需要的同学可参考 星座:我是...

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)'

根据日期判断星座函数

实例代码如下:

  1. function yige_constellation($month$day) { 
  2.  // 检查参数有效性  
  3.  if ($month < 1 || $month > 12 || $day < 1 || $day > 31) return false; 
  4.  
  5.  // 星座名称以及开始日期 
  6.  $constellations = array
  7.   array"20" => "宝瓶座"), 
  8.   array"19" => "双鱼座"), 
  9.   array"21" => "白羊座"), 
  10.   array"20" => "金牛座"), 
  11.   array"21" => "双子座"), 
  12.   array"22" => "巨蟹座"), 
  13.   array"23" => "狮子座"), 
  14.   array"23" => "处女座"), 
  15.   array"23" => "天秤座"), 
  16.   array"24" => "天蝎座"), 
  17.   array"22" => "射手座"), 
  18.   array"22" => "摩羯座"
  19.  ); 
  20.  
  21.  list($constellation_start$constellation_name) = each($constellations[(int)$month-1]); 
  22.  
  23.  if ($day < $constellation_start) list($constellation_start$constellation_name) = each($constellations[($month -2 < 0) ? $month = 11: $month -= 2]); 
  24.  
  25.  return $constellation_name

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

实例代码如下:

  1. /** 
  2. * 根据生日中的月份和日期来计算所属星座 
  3. * 
  4. * @param int $birth_month 
  5. * @param int $birth_date 
  6. * @return string 
  7. */ 
  8. function get_constellation($birth_month,$birth_date
  9. //判断的时候,为避免出现1和true的疑惑,或是判断语句始终为真的问题,这里统一处理成字符串形式 
  10. $birth_month = strval($birth_month); 
  11.  
  12. $constellation_name = array
  13.       '水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座'
  14.       '狮子座','处女座','天秤座','天蝎座)','射手座','摩羯座' 
  15.       ); 
  16.  
  17. if ($birth_date <= 22) 
  18.   if ('1' !== $birth_month
  19.   { 
  20.    $constellation = $constellation_name[$birth_month-2]; 
  21.   } 
  22.   else 
  23.   { 
  24.    $constellation = $constellation_name[11]; 
  25.   } 
  26. else 
  27.   $constellation = $constellation_name[$birth_month-1]; 
  28.  
  29. return $constellation
  30.  
  31. /** 
  32. * 根据生日中的年份来计算所属生肖 
  33. * 
  34. * @param int $birth_year 
  35. * @return string 
  36. */ 
  37. function get_animal($birth_year
  38. //1900年是子鼠年 
  39. $animal = array
  40.     '子鼠','丑牛','寅虎','卯兔','辰龙','巳蛇'
  41.     '午马','未羊','申猴','酉鸡','戌狗','亥猪' 
  42.     ); 
  43.  
  44. $my_animal = ($birth_year-1900)%12; 
  45. return $animal[$my_animal]; 
  46.  
  47. /** 
  48. * 根据生日来计算年龄 
  49. * 
  50. * 用Unix时间戳计算是最准确的,但不太好处理1970年之前出生的情况 
  51. * 而且还要考虑闰年的问题,所以就暂时放弃这种方式的开发,保留思想 
  52. * 
  53. * @param int $birth_year 
  54. * @param int $birth_month 
  55. * @param int $birth_date 
  56. * @return int 
  57. */ 
  58. function get_age($birth_year,$birth_month,$birth_date
  59. $now_age = 1; //实际年龄,以出生时为1岁计 
  60. $full_age = 0; //周岁,该变量放着,根据具体情况可以随时修改 
  61.  
  62. $now_year   = date('Y',time()); 
  63. $now_date_num  = date('z',time()); //该年份中的第几天 
  64. $birth_date_num = date('z',mktime(0,0,0,$birth_month,$birth_date,$birth_year)); 
  65.  
  66. $difference = $now_date_num - $birth_date_num
  67. if ($difference > 0) 
  68.   $full_age = $now_year - $birth_year
  69. else 
  70.   $full_age = $now_year - $birth_year - 1; 
  71.  
  72. $now_age = $full_age + 1; 
  73.  
  74. return $now_age
  75.  
  76. ?> 
分享到:
PHP本地与服务器获取时间方法 - php日期
PHP本地与服务器获取时间方法 很多朋友在获取日期时间时我们都是直接使用date而这个函数默认获取的日期是我们本地机器的日期时间了,那么要怎么获取服务器上的时间呢,下面我都总结一下. 代码如下: PHP 获取服务器时间:上面的方法只是获取本地时间,加上下面一行代码,便能获取服务器时间,代码...
PHP时间戳与日期之间转换 - php日期
PHP时间戳与日期之间转换 在php中如果要实现日期转换成时间戳我们可以直接使用strtotime函数,如果把时间戳转换成日期直接使用date()函数即可实现,下面我来给各们朋友介绍介绍. strtotime()函数 strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳. 实例代码如下:   ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……