獲取倆個日期間的所有日期
function getDay($start, $end)
{
$dtStart = strtotime($start);
$dtEnd = strtotime($end);
$day[] = date('Y-m-d', $dtStart);
while ($dtStart < $dtEnd) {
$dtStart = strtotime('+1 day', $dtStart);
$day[] = date('Y-m-d', $dtStart);
}
return $day;
}
eg:
$day = getDay('2021-07-22', '2021-07-27');
var_dump($day);
######################### 輸出
array(6) {
[0]=>
string(10) "2021-07-22"
[1]=>
string(10) "2021-07-23"
[2]=>
string(10) "2021-07-24"
[3]=>
string(10) "2021-07-25"
[4]=>
string(10) "2021-07-26"
[5]=>
string(10) "2021-07-27"
}
獲取每週的開始時間結束時間
function getWeekActionAndEnd($time = '', $first = 1)
{
if (!$time) {
$time = time();
}
$defaultDate = date("Y-m-d", $time);
$w = date('w', strtotime($defaultDate));
$days = $w ? $w - $first : 6;
$week_start = date('Y-m-d', strtotime("{$defaultDate} -{$days} days"));
$week_end = date('Y-m-d', strtotime("{$week_start} +6 days"));
return array("weekStart" => $week_start, "weekEnd" => $week_end);
}
eg:
$day = getWeekActionAndEnd(strtotime('2021-07-22'));
var_dump($day);
######################### 輸出
array(2) {
["weekStart"]=>
string(10) "2021-07-19"
["weekEnd"]=>
string(10) "2021-07-25"
}
獲取獲取一週日期
function getWeekArr($time = 0, $format = "Y-m-d")
{
if (!$time) {
$time = time();
}
$week = date('w', $time);
$weekName = array('星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日');
if (empty($week)) {
$week = 7;
}
$data = [];
for ($i = 0; $i <= 6; $i++) {
$currDay = $i + 1 - $week;
$data[$i]['date'] = date($format, strtotime("+{$currDay} days", $time));
$data[$i]['week'] = $weekName[$i];
}
return $data;
}
eg:
$day = getWeekArr(strtotime('2021-07-22'));
var_dump($day);
######################### 輸出
array(7) {
[0]=>
array(2) {
["date"]=>
string(10) "2021-07-19"
["week"]=>
string(9) "星期一"
}
[1]=>
array(2) {
["date"]=>
string(10) "2021-07-20"
["week"]=>
string(9) "星期二"
}
[2]=>
array(2) {
["date"]=>
string(10) "2021-07-21"
["week"]=>
string(9) "星期三"
}
[3]=>
array(2) {
["date"]=>
string(10) "2021-07-22"
["week"]=>
string(9) "星期四"
}
[4]=>
array(2) {
["date"]=>
string(10) "2021-07-23"
["week"]=>
string(9) "星期五"
}
[5]=>
array(2) {
["date"]=>
string(10) "2021-07-24"
["week"]=>
string(9) "星期六"
}
[6]=>
array(2) {
["date"]=>
string(10) "2021-07-25"
["week"]=>
string(9) "星期日"
}
}
獲取上個月第一天和最後一天
function getPMonthFirstDay($date) {
return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' -1 month'));
}
function getPMonthLastDay($date) {
return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' -1 day'));
}
獲取下個月第一天和最後一天
function getNextMonthFirstDay($date) {
return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month'));
}
function getNextMonthLastDay($date) {
return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +2 month -1 day'));
}
倆個日期相差的天數
function getDiffDays(string $startTime, string $endTime = '')
{
if (empty($endTime)) {
$endTime = date("y-m-d");
}
$datetimeStart = date_create($startTime);
$datetimeEnd = date_create($endTime);
$day = date_diff($datetimeStart, $datetimeEnd)->days;
return $day <= 0 ? 1 : $day;
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結