今日开始时间戳和结束时间戳
public static function today()
{
return [
'start' => mktime(0, 0, 0, date('m'), date('d'), date('Y')),
'end' => mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1,
];
}
昨日开始时间戳和结束时间戳
public static function yesterday()
{
return [
'start' => mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')),
'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
];
}
最近五天开始时间戳和结束时间戳
public static function five()
{
return [
'start' => mktime(0, 0, 0, date('m'), date('d') - 6, date('Y')),
'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
];
}
最近七天开始时间戳和结束时间戳
public static function seven()
{
return [
'start' => mktime(0, 0, 0, date('m'), date('d') - 7, date('Y')),
'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
];
}
最近30日开始时间戳和结束时间戳
public static function thirty()
{
return [
'start' => mktime(0, 0, 0, date('m'), date('d') - 30, date('Y')),
'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
];
}
这周开始时间戳和结束时间戳
public static function thisWeek()
{
$length = 0;
if (date('w') == 0) {
$length = 7;
}
return [
'start' => mktime(0, 0, 0, date('m'), date('d') - date('w') + 1 - $length, date('Y')),
'end' => mktime(23, 59, 59, date('m'), date('d') - date('w') + 7 - $length, date('Y')),
];
}
本月开始时间戳和结束时间戳
public static function thisMonth()
{
return [
'start' => mktime(0, 0, 0, date('m'), 1, date('Y')),
'end' => mktime(23, 59, 59, date('m'), date('t'), date('Y')),
];
}
今年开始时间戳和结束时间戳
public static function thisYear()
{
$start_month = 1;
$end_month = 12;
$start_time = date('Y') . '-' . $start_month . '-1 00:00:00';
$end_month = date('Y') . '-' . $end_month . '-1 23:59:59';
$end_time = date('Y-m-t H:i:s', strtotime($end_month));
return [
'start' => strtotime($start_time),
'end' => strtotime($end_time)
];
}
上个月开始时间戳和结束时间戳
public static function lastMonth()
{
$start = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
$end = mktime(23, 59, 59, date('m') - 1, date('t'), date('Y'));
if (date('m', $start) != date('m', $end)) {
$end -= 60 * 60 * 24;
}
return [
'start' => $start,
'end' => $end,
];
}
几个月前开始时间戳和结束时间戳
public static function monthsAgo($month)
{
return [
'start' => mktime(0, 0, 0, date('m') - $month, 1, date('Y')),
'end' => mktime(23, 59, 59, date('m') - $month, date('t'), date('Y')),
];
}
某年开始时间戳和结束时间戳
public static function aYear($year)
{
$start_month = 1;
$end_month = 12;
$start_time = $year . '-' . $start_month . '-1 00:00:00';
$end_month = $year . '-' . $end_month . '-1 23:59:59';
$end_time = date('Y-m-t H:i:s', strtotime($end_month));
return [
'start' => strtotime($start_time),
'end' => strtotime($end_time)
];
}
某月开始时间戳和结束时间戳
public static function aMonth($year = 0, $month = 0)
{
$year = $year ?? date('Y');
$month = $month ?? date('m');
$day = date('t', strtotime($year . '-' . $month));
return [
"start" => strtotime($year . '-' . $month),
"end" => mktime(23, 59, 59, $month, $day, $year)
];
}
获取两时间戳相差的时间
public static function timediff($begin_time, $end_time)
{
if ($begin_time < $end_time) {
$starttime = $begin_time;
$endtime = $end_time;
} else {
$starttime = $end_time;
$endtime = $begin_time;
}
$timediff = $endtime - $starttime;
$days = intval($timediff / 86400);
$remain = $timediff % 86400;
$hours = intval($remain / 3600);
$remain = $remain % 3600;
$mins = intval($remain / 60);
$secs = $remain % 60;
$res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
return $res;
}
获取本年所有月份,开始和结束时间戳
public static function getMonth()
{
$year = date('Y');
$yeararr = [];
$month = [];
for ($i=1; $i <=12 ; $i++) {
$yeararr[$i] = $year.'-'.$i;
}
foreach ($yeararr as $key => $value) {
$timestamp = strtotime( $value );
$start_time = date( 'Y-m-1 00:00:00', $timestamp );
$mdays = date( 't', $timestamp );
$end_time = date( 'Y-m-' . $mdays . ' 23:59:59', $timestamp );
$month[$key]['start_time'] = $start_time;
$month[$key]['end_time'] = $end_time;
}
return $month;
}