有点长阿,费了1天多的功夫了,里面需要很多技巧,阳历转阴历的函数我是从网上找的,改造 了一下.
基本上用到创建一个CCK字段块所涉及到的各个钩子了.
看到这么多行代码,应该知道还是花费老葛不少精力的.
希望这些代码能够带动更多的人学习CCK字段模块的创建,这里面需要很多Drupal 技能的,希望这些代码,能够帮助那些有这方面的需求,但是不知道如何实现的 朋友,降低成本.也希望能够起到抛砖引玉的效果.
本课程的教学视频,明天就会整理好上传上来.
<?php
// $Id$
/**
* @file
* 定义一个简单的阴历日期字段.
*/
/**
* Implementation of hook_theme().
*/
function lunar_theme() {
return array(
'lunar_textfield' => array(
'arguments' => array('element' => NULL),
),
'lunar_formatter_default' => array(
'arguments' => array('element' => NULL),
),
'lunar_formatter_solar' => array(
'arguments' => array('element' => NULL),
),
'lunar_formatter_lunar' => array(
'arguments' => array('element' => NULL),
),
);
}
/**
* Implementation of hook_field_info().
*/
function lunar_field_info() {
return array(
'lunar' => array(
'label' => t('阴历'),
'description' => t('在数据库中存储日期的阴历形式。'),
),
);
}
/**
* Implementation of hook_field_settings().
*/
function lunar_field_settings($op, $field) {
switch ($op) {
case 'database columns':
return array(
'solar_date' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'sortable' => TRUE),
'lunar_date' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'sortable' => TRUE),
);
}
}
/**
* Implementation of hook_field().
*/
function lunar_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
if (is_array($items)) {
foreach ($items as $delta => $item) {
if ($item['solar_date'] != '' && !_valid_solar_date(trim($item['solar_date']))) {
form_set_error($field['field_name'],t('"%date" is not a valid solar date',array('%date' => $item['solar_date'])));
}
}
}
break;
case 'presave':
foreach ($items as $delta => $value) {
_lunar_process($items[$delta], $delta, $field, $node);
}
break;
case 'sanitize':
foreach ($items as $delta => $item) {
$items[$delta]['safe'] = check_plain($item['solar_date']);
}
break;
}
}
function _valid_solar_date($solar_date){
return preg_match("/^((((19|20)\d{2})-(0?[13-9]|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/", $solar_date);
//return TRUE;
}
/**
* Implementation of hook_widget_info().
*/
function lunar_widget_info() {
return array(
'lunar_textfield' => array(
'label' => t('阴历'),
'field types' => array('lunar'),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_DEFAULT,
),
),
);
}
/**
* Implementation of hook_widget().
*/
function lunar_widget(&$form, &$form_state, $field, $items, $delta = 0) {
$element = array(
'#type' => $field['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
'#title' => $field['widget']['label'],
'#weight' => $field['widget']['weight'],
'#description' => $field['widget']['description'],
'#required' => $field['required'],
'#field' => $field,
);
return $element;
}
/**
* Implementation of hook_widget_settings().
*/
function lunar_widget_settings($op, $widget) {
switch ($op) {
case 'form':
$size = (isset($widget['size']) && is_numeric($widget['size'])) ? $widget['size'] : 60;
$form['size'] = array(
'#type' => 'textfield',
'#title' => t('Size of textfield'),
'#default_value' => $size,
'#element_validate' => array('_lunar_widget_settings_size_validate'),
'#required' => TRUE,
);
return $form;
case 'save':
return array('size');
}
}
function _lunar_widget_settings_size_validate($element, &$form_state) {
$value = $form_state['values']['size'];
if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
form_error($element, t('"Size" must be a positive integer.'));
}
}
/**
* Implementation of FAPI hook_elements().
*/
function lunar_elements() {
return array(
'lunar_textfield' => array(
'#input' => TRUE,
'#columns' => array('solar_date','lunar_date'),
'#delta' => 0,
'#process' => array('lunar_textfield_process'),
),
);
}
function lunar_textfield_process($element, $edit, $form_state, $form){
$field = $form['#field_info'][$element['#field_name']];
//$field_key = $element['#columns'][0];
$delta = $element['#delta'];
$element['solar_date'] = array(
'#type' => 'textfield',
'#title' => t('阳历日期'),
'#description' => t('格式为2009-05-05'),
'#required' => $element['#required'],
'#maxlength' => 255,
'#size' => !empty($field['widget']['size']) ? $field['widget']['size'] : 60,
'#attributes' => array('class' => 'text'),
'#default_value' =>isset($element['#value']['solar_date']) ? $element['#value']['solar_date'] : NULL,
);
$element['lunar_date'] = array(
'#type' => 'hidden',
'#default_value' =>isset($element['#value']['lunar_date']) ? $element['#value']['lunar_date'] : NULL,
);
return $element;
}
/**
* Implementation of hook_field_formatter_info().
*/
function lunar_field_formatter_info() {
return array(
'default' => array(
'label' => t('阳历,阴历同时显示'),
'field types' => array('lunar'),
'multiple values' => CONTENT_HANDLE_CORE,
),
'solar' => array(
'label' => t('只显示阳历'),
'field types' => array('lunar'),
'multiple values' => CONTENT_HANDLE_CORE,
),
'lunar' => array(
'label' => t('只显示阴历'),
'field types' => array('lunar'),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
}
function theme_lunar_textfield($element){
return $element['#children'];
}
function lunar_content_is_empty($item, $field){
if (empty($item['solar_date'])) {
return TRUE;
}
return FALSE;
}
/**
* Theme function for 'default' text field formatter.
*/
function theme_lunar_formatter_default($element) {
return $element['#item']['solar_date']." ".$element['#item']['lunar_date'];
}
function theme_lunar_formatter_solar($element) {
return $element['#item']['solar_date'];
}
function theme_lunar_formatter_lunar($element) {
return $element['#item']['lunar_date'];
}
function solar2lunar($solar){
$lunar_array =null;
$lunar = null;
$date_array = explode('-',$solar);
$lunar_array = getLunarCalendar($date_array[0],$date_array[1], $date_array[2]);
$lunar = $lunar_array['yearname'].$lunar_array['displaymonth'].$lunar_array['displayday'];
return $lunar;
}
function _lunar_process(&$item, $delta = 0, $field, $node){
$item['solar_date'] = trim($item['solar_date']);
$item['lunar_date'] = solar2lunar($item['solar_date']);
}
function getLunarCalendar($year, $month, $day) {
// 农历每月的天数
$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;
$year = intval($year);
$month = intval($month);
$day = intval($day);
if ($year < 1901 || $year > 2020 || $month < 1 || $month > 12 || $day < 1 || $day > 31) {
exit('年份或者格式出错,年份只支持1901到2020!');
}
// 获得日期时间戳
$postDate = mktime(0, 0, 0, $month, $day, $year);
$thisDay = getdate($postDate);
//print_R($thisDay);exit;
$cur_wday = $thisDay["wday"];
for($y = 1901; $y < $thisDay["year"]; $y++) {
// 计算到所求日期阳历的总天数-自1900年12月21日始,先算年的和
$total += 365;
if ($y % 4 == 0){
$total++;
}
}
switch ($thisDay["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 ($thisDay["year"] % 4 == 0 && $thisDay["mon"] > 2) {
$total++; //如果当年是闰年还要加一天
}
$total = $total + $thisDay["mday"]-1; //加当月的天数
$flag = 0; //判断跳出循环的条件
$j = 0;
while ($j <= 120) { // 用农历的天数累加来判断是否超过阳历的天数
$i = 1;
while ($i <= 13) {
$mtotal += $everymonth[$j][$i];
if ($mtotal >= $total) {
$flag = 1;
break;
}
$i++;
}
if ($flag == 1) break;
$j++;
}
if ($everymonth[$j][0] <> 0 && $everymonth[$j][0] < $i) {
//对闰月修补
$mm = $i-1;
} else {
$mm = $i;
}
if ($i == $everymonth[$j][0] + 1 && $everymonth[$j][0] <> 0) {
$nlmon = $mmonth[0] . $mmonth[$mm]; #闰月
$numMonth = $mm;//输出农历数字格式月份
} else {
$nlmon = $mmonth[$mm] . $mmonth[13];
$numMonth = $mm;
}
// 计算所求月份1号的农历日期
$md = $everymonth[$j][$i] - ($mtotal - $total);
if ($md > $everymonth[$j][$i]) {
$md -= $everymonth[$j][$i];
}
$nlday = $mday[$md];
$numDay = $md;//输出农历数字格式日期
$nowday = date("Y年n月j日 ", $postDate) . $weekday[$cur_wday]."
".$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]]."年".$nlmon.$nlday;
$lunarCalendar = array('year'=>$year,
'yearname'=>$mten[$everymonth[$j][14]] . $mtwelve[$everymonth[$j][15]] . "年",
'displaymonth'=>$nlmon,
'displayday'=>$nlday,
'displayweek'=>$weekday[$cur_wday],
'month'=>$numMonth,
'day'=>$numDay);
return $lunarCalendar;
}
评论
老葛:我借了你的getLunarCalendar函数
我借用了你的getLunarCalendar函数,用在"为calendar增加农历显"
http://joom.net.ru/content/wei-calendarzeng-jia-nong-li-xian-shi
希望别介意 :)
这个函数我也是从网上找的
这个函数我也是从网上找的
应该是你改造过的
网上我也找了一圈,看代码结构都不怎么清楚,还是你这个清楚一点
不过,如果能加上节气与一般的农历节日就更好了
这个函数确实是拷贝别人的
这个函数确实是拷贝别人的,只是把中文的双引号改造成了英文的双引号,其它没有动.网上也有其它的代码,这段代码只是一个函数,引用比较方便.而且他没有注明作者,商用什么的.
网上有段其它的代码,商用要联系原作者.所以就没有用.代码我只做了简单的测试,就是输入了几个日期,返回的结果都是对的.