日期操作
一、根据日期生成日期数组
php
/**
* 根据指定日期生成日期数组
* @param $start_time 开始时间
* @param $end_time 结束时间
* @return array 返回数组结果
*/
function createDateArr($start_time, $end_time)
{
$open_start_time = $start_time;
$open_end_time = $end_time;
$stimestamp = strtotime($open_start_time);
$etimestamp = strtotime($open_end_time);
// 计算日期段内有多少天
$days = ($etimestamp - $stimestamp) / 86400 + 1;
// 保存每天日期
$arr = [];
for ($i = 0; $i < $days; $i++) {
$arr[] = date('Y-m-d', $stimestamp + (86400 * $i));
}
return $arr;
}
//调用示例
$start_time='2024-05-12';
$end_time='2024-05-15';
$data=createDateArr($start_time,$end_time);
//结果
Array
(
[0] => 2024-05-12
[1] => 2024-05-13
[2] => 2024-05-14
[3] => 2024-05-15
)
二、根据指定日期获取月份数组
php
/**
* 根据指定日期生成月份数组
* @param $startDate
* @param $endDate
* @return array
* @throws Exception
*/
function generateMonthlyArray($startDate, $endDate) {
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$end->modify('last day of this month');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
$monthlyArray = array();
foreach ($period as $month) {
$monthlyArray[] = $month->format('Y-m');
}
return $monthlyArray;
}
//调用示例
$start_time='2024-05-12';
$end_time='2024-12-15';
$data=generateMonthlyArray($start_time,$end_time);
//结果
Array
(
[0] => 2024-05
[1] => 2024-06
[2] => 2024-07
[3] => 2024-08
[4] => 2024-09
[5] => 2024-10
)
三、根据指定时间戳返回一周之内的数组数据
php
/**
* 返回指定时间戳一周之内的数组数据
* @param string $time 需要转换的时间戳
* @param string $format 返回日期格式
* @return array
*/
function get_week($time = '', $format = 'Y-m-d')
{
$time = $time != '' ? $time : time();
//获取当前周几
$week = date('w', $time);
$date = [];
for ($i = 1; $i <= 7; $i++) {
$date[$i] = date($format, strtotime('+' . $i - $week . ' days', $time));
}
return $date;
}
//调用示例
$time=strtotime('2024-05-07');
$format='Y/m/d';
$data=get_week($time,$format);
//结果
Array
(
[1] => 2024/05/06
[2] => 2024/05/07
[3] => 2024/05/08
[4] => 2024/05/09
[5] => 2024/05/10
[6] => 2024/05/11
[7] => 2024/05/12
)
四、将指定日期转化为星期几
php
/**
* 将指定日期转化成星期几
* @param $date
* @return string
*/
function get_week($date)
{
header("Content-type: text/html; charset=utf-8");
//强制转换日期格式
$date_str = date('Y-m-d', strtotime($date));
//封装成数组
$arr = explode("-", $date_str);
//参数赋值
//年
$year = $arr[0];
//月,输出2位整型,不够2位右对齐
$month = sprintf('%02d', $arr[1]);
//日,输出2位整型,不够2位右对齐
$day = sprintf('%02d', $arr[2]);
//时分秒默认赋值为0;
$hour = $minute = $second = 0;
//转换成时间戳
$strap = mktime($hour, $minute, $second, $month, $day, $year);
//获取数字型星期几
$number_wk = date("w", $strap);
//自定义星期数组
$weekArr = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
//获取数字对应的星期
return $weekArr[$number_wk];
}
//调用示例
$time='2024-05-07';
$data=get_week($time);
//结果
星期二
五、计算俩个日期之差
php
/**
* 计算俩个日期之差
* @param $day1 开始日期
* @param $day2 结束日期
* @return float|int 返回天数
*/
function diffBetweenTwoDays($day1, $day2)
{
$second1 = strtotime($day1);
$second2 = strtotime($day2);
if ($second1 < $second2) {
$tmp = $second2;
$second2 = $second1;
$second1 = $tmp;
}
return ($second1 - $second2) / 86400;
}
//调用示例
$start_time='2024-05-07';
$end_time='2024-05-08';
$data=diffBetweenTwoDays($start_time,$end_time);
//结果
1
六、根据将秒数转换成年月日时分秒
php
/**
* 根据将秒数转换成年月日时分秒
* @param $time
* @return string
*/
function GEtTime($time){
if(is_numeric($time)){
$value = array(
"years" => 0, "days" => 0, "hours" => 0,
"minutes" => 0, "seconds" => 0,
);
if($time >= 31556926){
$value["years"] = floor($time/31556926);
$time = ($time%31556926);
}
if($time >= 86400){
$value["days"] = floor($time/86400);
$time = ($time%86400);
}
if($time >= 3600){
$value["hours"] = floor($time/3600);
$time = ($time%3600);
}
if($time >= 60){
$value["minutes"] = floor($time/60);
$time = ($time%60);
}
$value["seconds"] = floor($time);
$n=$t=$h=$m=$s='';
if ($value["years"]>0){
$n=$value["years"]."年";
}
if ($value["days"]>0){
$t=$value["days"]."天";
}
if ($value["hours"]>0){
$h=$value["hours"]."小时";
}
if ($value["minutes"]>0){
$m=$value["minutes"]."分";
}
if ($value["seconds"]>0){
$s=$value["seconds"]."秒";
}
$time2=$n.$t.$h.$m.$s;
return $time2;
}else{
return '时间格式不正确';
}
}
//调用示例
$time=32556926;
$time=80;
$data=GEtTime($time);
//结果
1年11天13小时46分40秒
1分20秒
七、获取今日当月今年日期
php
//注意 空格不能去掉
//获取今日日期0点-23.59
$time = date('Y-m-d', time());
$start_time=$time.' 00:00:00';
$end_time = $time." 23:59:59";
//获取当月日期
$start_time = date('Y-m-01', time()) . ' 00:00:00';
$end_time = date('Y-m-d', strtotime("$start_time+1 month -1 day")) . ' 23:59:59';
//获取今年日期
$start_time = date('Y-01-01', time());
$end_time = date('Y-m-d', strtotime("$start_time+1 year -1 day"));
时间转换
一、将日期转换成距今多长时间
php
/**
* 将日期转换成距今多长时间
* @param $show_time 日期时间
* @param $now_time 当前时间
* @return false|int|string
*/
function time_distance($show_time)
{
if (!$show_time) {
return '';
}
$show_time = (!is_numeric($show_time)) ? strtotime($show_time) : $show_time;
$now_time = time();
$dur = $now_time - $show_time;
if ($dur < 0) {
return $show_time;
} else {
if ($dur < 10) {
return '刚刚';
} else if ($dur < 60) {
return $dur . '秒前';
} else {
if ($dur < 3600) {
return floor($dur / 60) . '分钟前';
} else {
if ($dur < 86400) {
return floor($dur / 3600) . '小时前';
} else {
return date_change($show_time);
}
}
}
}
}
//转变时间
function date_change($date)
{
if (!$date) {
return '';
}
$date = (!is_numeric($date)) ? strtotime($date) : $date;
if (date('Ymd', $date) == date('Ymd', time())) {
return date('H:i', $date);
} else if (date('Ymd', $date) == date('Ymd', strtotime('-1 day'))) {
return "昨天 " . date('H:i', $date);
} else if (date('Y', $date) == date('Y', time())) {
return date('m月d日', $date);
} else {
return date('Y年m月d日', $date);
}
}
//示例
$start_time='2024-05-31 16:23:56';
$data= time_distance($start_time);
//结果
55分钟前
1小时前