會員活動 迴圈獲取會員日時間
會員活動 迴圈獲取會員日時間並且返回最近一次會員日時間
if (!function_exists('get_circulate_timestamp')) {
function get_circulate_timestamp($dates, $date_type, $reference_timestamp)
{
$timestamp_array = [];
$format = '';
switch ($date_type) {
case 1:
$format = 'Y-';
break;
case 2:
$format = 'Y-m-';
break;
case 3:
$week = date("w", $reference_timestamp);
if ($week == 0) {
$week = 7;
}
$current_time = strtotime(date("Y-m-d", $reference_timestamp));
break;
default:
return [];
}
foreach ($dates as $date_info) {
switch ($date_type) {
case 1:
case 2:
$timestamp_array[] = strtotime($format . $date_info);
break;
case 3:
$timestamp_array[] = $current_time + ($date_info - $week) * 86400;
break;
}
}
return $timestamp_array;
}
}
if (!function_exists('get_time_array')) {
function get_time_array($dates, $date_type, $reference_timestamp, $compare_timestamp)
{
$timestamp_array = get_circulate_timestamp($dates, $date_type, $reference_timestamp);
$time_array = ['next_begin' => 0, 'nearest_begin' => 0];
foreach ($timestamp_array as $timestamp) {
if ($timestamp > $compare_timestamp) {
$time_array['next_begin'] = ($time_array['next_begin'] == 0 || $timestamp < $time_array['next_begin']) ? $timestamp : $time_array['next_begin'];
}
if ($timestamp <= $compare_timestamp) {
$time_array['nearest_begin'] = ($time_array['nearest_begin'] == 0 || $timestamp > $time_array['nearest_begin']) ? $timestamp : $time_array['nearest_begin'];
}
}
return $time_array;
}
}
獲取日周月年 時間段時間戳
獲取當前 日/周/月/年 起始和結束時間戳
獲取前一 日/周/月/年 起始和結束時間戳
function get_period_timestamp($start_time, $period_type = 'daily')
{
switch ($period_type) {
case 'daily':
//所選時間的昨天起始
$before_begin = date('Y-m-d 00:00:00', strtotime('-1 day', $start_time));
$before_end = date('y-m-d 23:59:59', strtotime($before_begin));
//所選時間的一天起始
$begin = strtotime(date('Y-m-d', $start_time));
$end = strtotime(date('Y-m-d 23:59:59', $start_time));
break;
case 'weekly':
//上週一
if (date('w') == 1) {
$before_begin = date('Y-m-d', strtotime('last monday', $start_time)); //無論今天幾號,-1 monday為上一個有效周
} else {
$before_begin = date('Y-m-d', strtotime('-1 week last monday', $start_time)); //無論今天幾號,-1 monday為上一個有效周
}
//上週日
$before_end = date('Y-m-d 23:59:59', strtotime('-1 sunday', $start_time)); //上一個有效週日,同樣適用於其它星期
//本週一
// $begin = date('Y-m-d', ($start_time - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)); //w為星期幾的數字形式,這裡0為週日
$begin = date('Y-m-d', strtotime('this week Monday', $start_time));
$end = date('Y-m-d', strtotime('+1week', $begin)) - 1;
break;
case 'month':
//上月一日
$before_begin = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', $start_time) . '-01 00:00:00'))); //本月一日直接strtotime上減一個月
//上月最後一日
$before_end = date('Y-m-d 23:59:59', strtotime(date('Y-m', $start_time) . '-01 00:00:00') - 86400); //本月一日減一天即是上月最後一日
//所選時間的月起始
$begin = date('Y-m-d', strtotime(date('Y-m', $start_time) . '-01 00:00:00')); //直接以strtotime生成
$begin = date('Y-m-d', strtotime(date('Y-m-01', $start_time)));
$end = date('Y-m-d', strtotime('+1month', $begin)) - 1;
break;
case 'year':
//去年一月一日
$before_begin = date('Y-m-d', strtotime('-1 year', strtotime(date('Y', $start_time) . '-01-01 00:00:00'))); //本月一日直接strtotime上減一個月
//去年十二月三十一日
$before_end = date('Y-m-d 23:59:59', strtotime(date('Y', $start_time) . '-01-01 00:00:00') - 1); //本月一日減一天即是上月最後一日
//所選時間的年起始\
$begin = date('Y-m-d', strtotime(date('Y-01-01', $start_time)));
$end = date('Y-m-d', strtotime('+1year', $begin)) - 1;
break;
default:
return build_result('型別錯誤 :daily,weekly,month,year');
}
$result = [
'before_begin' => $before_begin,
'before_end' => $before_end,
'begin' => $begin,
'end' => $end
];
return build_result('查詢成功', 0, $result);
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結