This paper describes the Yii framework to achieve by day, month, year, custom time period statistics method. The details are as follows:
Day: format y-m-d
Month: format y-m
Year: format y
Range: format y-m-d
First calculate the time
0-23 hours per day
$rangeTime = range(0, 23);
Month: January to the end of the month
// $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$days = date("t",strtotime($year . '-' . $month));
//Days to generate 1-days
$rangeTime = range(1, $days);
Year: January to December
$rangeTime = range(1, 12);
Time period; start end time
$stimestamp = strtotime($time);
$etimestamp = strtotime($time2);
//How many days are there in the calculation period
$days = ($etimestamp - $stimestamp) / 86400 + 1;
//Save daily date
for($i = 0; $i < $days; $i++){
$newTimeStamp = $stimestamp + (86400 * $i);
$rangeTime[] = date('Y-m-d', $newTimeStamp);
$labels[] = date('d', $newTimeStamp) . Yii::t('backend', 'day');
}
Wrap it up
/**
*Get the label and time period
* type: day, month, year, range
*Day is the specific day, y-m-d, month is the specific month, y-m, and year is the specific year
*Time2 date, the second time of the period
*/
public function getLabelAndRangeTime($type, $time, $time2) {
if(empty($time)) {
$time = date('Y-m-d', time());
}
$labels = [];
$rangeTime = [];
if($type == 'day') {
//Generate 1-24 hours
$rangeTime = range(0, 23);
foreach ($rangeTime as $key => $val) {
$label = $val . Yii::t('backend', 'hour');
$labels[] = $label;
}
} else if($type == 'month') {
$dateArr = explode('-', $time);
if(count($dateArr > 1)) {
$year = $dateArr[0];
$month = $dateArr[1];
$time = $year;
$time2 = $month;
//Gets the number of days in the current month
// $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$days = date("t",strtotime($year . '-' . $month));
//Days to generate 1-days
$rangeTime = range(1, $days);
foreach ($rangeTime as $key => $val) {
$label = $val . Yii::t('backend', 'day');
$labels[] = $label;
}
}
} else if($type == 'year') {
//Generated from January to December
$rangeTime = range(1, 12);
foreach ($rangeTime as $key => $val) {
$label = $val . Yii::t('backend', 'month');
$labels[] = $label;
}
} else if($type == 'range') {
$stimestamp = strtotime($time);
$etimestamp = strtotime($time2);
//How many days are there in the calculation period
$days = ($etimestamp - $stimestamp) / 86400 + 1;
//Save daily date
for($i = 0; $i < $days; $i++){
$newTimeStamp = $stimestamp + (86400 * $i);
$rangeTime[] = date('Y-m-d', $newTimeStamp);
$labels[] = date('d', $newTimeStamp) . Yii::t('backend', 'day');
}
}
return [
'type' => $type,
'time' => $time,
'time2' => $time2,
'rangeTime' => $rangeTime,
'labels' => $labels
];
}
Then query the database
$query = Order::find();
if($type == 'day') {
$query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d %H") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount'])
->where(['FROM_UNIXTIME(pay_at,"%Y-%m-%d")' => $time]);
} else if($type == 'month') {
$query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount'])
->where(['FROM_UNIXTIME(pay_at,"%Y-%m")' => ($time . '-' . $time2)]);
} else if ($type == 'year') {
$query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount'])
->where(['FROM_UNIXTIME(pay_at,"%Y")' => $time]);
} else if ($type == 'range') {
$query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount'])
->where(['between', 'FROM_UNIXTIME(pay_at,"%Y-%m-%d")', $time, $time2]);
}
$data = $query->andWhere(['pay_status' => 2])->groupBy('char_time')->all();
In chronological order
$dataArr = [];
foreach ($data as $allKey => $allVal) {
$dataArr[$allVal->char_time]['char_time'] = $allVal->char_time;
$dataArr[$allVal->char_time]['total_order'] = $allVal->total_order;
$dataArr[$allVal->char_time]['total_order_amount'] = bcdiv($allVal->total_order_amount, 100, 2);
}
Then get the corresponding data according to the time
foreach ($rangeTime as $key => $val) {
if($type == 'range') {
if (array_key_exists($val, $dataArr)) {
$charCountDatas[] = $dataArr[$val]['total_order'];
$charAmountDatas[] = $dataArr[$val]['total_order_amount'];
} else {
$charCountDatas[] = 0;
$charAmountDatas[] = 0;
}
} else {
$theNow = strlen($val) == 2 ? $val : '0' . $val;
if($type == 'day') {
$theTime = $time . ' ' . $theNow;
} else if($type == 'month') {
$theTime = $time . '-' . $time2 . '-' . $theNow;
} else if($type == 'year') {
$theTime = $time . '-' . $theNow;
}
if (array_key_exists($theTime, $dataArr)) {
$charCountDatas[] = $dataArr[$theTime]['total_order'];
$charAmountDatas[] = $dataArr[$theTime]['total_order_amount'];
} else {
$charCountDatas[] = 0;
$charAmountDatas[] = 0;
}
}
}
Under the package
/**
*Order quantity and amount paid in time period
*Type: day, month, year
*Time: time, day: selected time; month: year: year; range: first time
*Time2: time: Day: '; month: month; year:'; range: the second time
*Range time: Day: 1-24 hours; month: 1-30 days; year: 1-12 months; range: days between time and time2
*/
public function getDayOrderPayChar($type, $time, $time2, $rangeTime) {
$query = Order::find();
if($type == 'day') {
$query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d %H") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount'])
->where(['FROM_UNIXTIME(pay_at,"%Y-%m-%d")' => $time]);
} else if($type == 'month') {
$query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount'])
->where(['FROM_UNIXTIME(pay_at,"%Y-%m")' => ($time . '-' . $time2)]);
} else if ($type == 'year') {
$query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount'])
->where(['FROM_UNIXTIME(pay_at,"%Y")' => $time]);
} else if ($type == 'range') {
$query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount'])
->where(['>=', 'FROM_UNIXTIME(pay_at,"%Y-%m-%d")', $time])
->andWhere(['<=', 'FROM_UNIXTIME(pay_at,"%Y-%m-%d")', $time2]);
}
$data = $query->andWhere(['pay_status' => 2])->groupBy('char_time')->all();
$dataArr = [];
foreach ($data as $allKey => $allVal) {
$dataArr[$allVal->char_time]['char_time'] = $allVal->char_time;
$dataArr[$allVal->char_time]['total_order'] = $allVal->total_order;
$dataArr[$allVal->char_time]['total_order_amount'] = bcdiv($allVal->total_order_amount, 100, 2);
}
$charCountDatas = [];
$charAmountDatas = [];
foreach ($rangeTime as $key => $val) {
if($type == 'range') {
if (array_key_exists($val, $dataArr)) {
$charCountDatas[] = $dataArr[$val]['total_order'];
$charAmountDatas[] = $dataArr[$val]['total_order_amount'];
} else {
$charCountDatas[] = 0;
$charAmountDatas[] = 0;
}
} else {
$theNow = strlen($val) == 2 ? $val : '0' . $val;
if($type == 'day') {
$theTime = $time . ' ' . $theNow;
} else if($type == 'month') {
$theTime = $time . '-' . $time2 . '-' . $theNow;
} else if($type == 'year') {
$theTime = $time . '-' . $theNow;
}
if (array_key_exists($theTime, $dataArr)) {
$charCountDatas[] = $dataArr[$theTime]['total_order'];
$charAmountDatas[] = $dataArr[$theTime]['total_order_amount'];
} else {
$charCountDatas[] = 0;
$charAmountDatas[] = 0;
}
}
}
$res = [
'count' => [
'name' => Yii::t('backend', 'hour_order_pay_count_title'),
'color' => '#99CC33',
'charData' => $charCountDatas
],
'amount' => [
'name' => Yii::t('backend', 'hour_order_pay_amount_title'),
'color' => '#99CC33',
'charData' => $charAmountDatas
]
];
return $res;
}
front end
<div>
<div>
<div>
<div>
<?= Html::dropDownList('day_type', $type, ['day' => Yii::t('backend', 'day'), 'month' => Yii::t('backend', 'month'), 'year' => Yii::t('backend', 'year'), 'range' => Yii::t('backend','range_time')], ['class' => 'type dashboard-time-type']) ?>
</div>
<div>
<div>
<div>
<?= DateTimePicker::widget([
'name' => 'time',
'value' => (!empty($time) && $type == 'day') ? $time : '',
'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time'],
'removeButton' => false,
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'startView' => 'month',
'minView' => 'month',
'maxView' => 'month',
'autoclose' => true
]
]) ?>
</div>
<div>
<?= DateTimePicker::widget([
'name' => 'time',
'value' => (!empty($time) && $type == 'month') ? $time : '',
'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time'],
'removeButton' => false,
'pluginOptions' => [
'format' => 'yyyy-mm',
'startView' => 'year',
'minView' => 'year',
'maxView' => 'year',
'autoclose' => true
]
]) ?>
</div>
<div>
<?= DateTimePicker::widget([
'name' => 'time',
'value' => (!empty($time) && $type == 'year') ? $time : '',
'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time'],
'removeButton' => false,
'pluginOptions' => [
'format' => 'yyyy',
'startView' => 'decade',
'minView' => 'decade',
'maxView' => 'decade',
'autoclose' => true
]
]) ?>
</div>
<div>
<div>
<div>
<?= DateTimePicker::widget([
'name' => 'time',
'value' => (!empty($time) && $type == 'range') ? $time : '',
'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time time2'],
'removeButton' => false,
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'startView' => 'month',
'minView' => 'month',
'maxView' => 'month',
'autoclose' => true
]
]) ?>
</div>
<div>
<?= DateTimePicker::widget([
'name' => 'time2',
'value' => (!empty($time2) && $type == 'range') ? $time2 : '',
'options' => ['placeholder' => Yii::t('backend', 'date'), 'autocomplete' => 'off', 'class' => 'time time2'],
'removeButton' => false,
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'startView' => 'month',
'minView' => 'month',
'maxView' => 'month',
'autoclose' => true
]
]) ?>
</div>
</div>
</div>
</div>
</div>
<div>
<?= Html::button(Yii::t('backend', 'sure'), ['class' => 'btn btn-success btn-dashboard-time', 'data-url' => $url]) ?>
</div>
</div>
</div>
</div>
confirm button
$('.dashboard-time-select .btn-dashboard-time').click(function() {
var url = $(this).attr('data-url');
var timeSelect = $(this).parent().parent();
var type = timeSelect.find('.type').val();
var time = '';
var time2 = '';
if(type == 'day') {
time = timeSelect.find('.dashboard-time-day input').val();
} else if(type == 'month') {
time = timeSelect.find('.dashboard-time-month input').val();
} else if(type == 'year') {
time = timeSelect.find('.dashboard-time-year input').val();
} else if(type == 'range') {
time = timeSelect.find('.dashboard-time-range .range-start input').val();
time2 = timeSelect.find('.dashboard-time-range .range-end input').val();
}
window.location.href = baseBackend + '/' + url + '?type=' + type + '&time=' + time + '&time2=' + time2
})
$('.dashboard-time-select .dashboard-time-type').change(function() {
var type = $(this).val();
$('.dashboard-time-select .dashboard-time-picker').addClass('hide');
$('.dashboard-time-select .dashboard-time-' + type).removeClass('hide');
})
For more information about Yii, readers who are interested in it can see the following topics: Yii framework introduction and common skills summary, PHP excellent development framework summary, smart template introduction basic course, PHP object-oriented programming introduction course, PHP string usage summary, PHP + MySQL database operation introduction course and PHP common database operation introduction course Summary of writing skills
I hope this article will be helpful to the PHP Programming Based on Yii framework.