您正在使用 IPV4 [44.215.110.142] 访问本站,您本次已经查看了 1 页
用户名: 密 码: 验证码:     用QQ登录本站
首页 软件 编程 笑话 知识 公告 台风 日历 计算器
悟空收录网       [公益]保护绿色环境,构建和谐社会      

【腾讯云】 爆款2核2G3M云服务器首年 61元,叠加红包再享折上折      
[公益] 地球是我家,绿化靠大家      
2024年 清明节 007
2024年 劳动节 034
2025年 元 旦 279
2025年 春 节 307
 
您现在的位置:首页 >> PHP >> 内容
本类新增
本类热门
PHP常用函数之格式化时间操作示例
内容摘要: /***格式化时间*@param$time时间戳*@returnbool|string*/functionformatTimeToNow($time){//获取现在的时间戳$nowtime=time();if($time$nowtime){return'';}else{$tc=$nowtime-$time;if($tc=864000){$str=date('......
/**

*格式化时间

*@param$time时间戳

*@returnbool|string

*/

functionformatTimeToNow($time){

//获取现在的时间戳

$nowtime=time();

if($time>$nowtime){

return'';

}else{

$tc=$nowtime-$time;

if($tc>=864000){

$str=date('Y-m-dH:i',$time);//如果大于10天,则直接显示日期

}elseif($tc>=86400){

$str=floor($tc/86400)."天前";//如果大于1天

}elseif($tc>=3600){

$str=floor($tc/3600)."小时前";//如果大于1小时

}elseif($tc>=60){

$str=floor($tc/60)."分钟前";//如果大于1分钟

}else{

$str="刚刚";

}

return$str;

}

}

/**

*将中文的日期格式化为正常的日期

*@param$date

*@returnmixed

*/

functionformatCnDateToDate($date){

//把年月替换为-,日替换为空

$date=str_replace('年','-',$date);

$date=str_replace('月','-',$date);

$date=str_replace('日','',$date);

//避免提交的格式不统一,例如2018-3-2等,标准化

returndate('Y-m-d',strtotime($date));

}

/**

*计算自然周期的开始时间戳和结束时间戳(周一到周日,月初到月末)

*@paramint$time_type1表示自然天,2表示自然周,3表示自然月

*@paramint$prev_num距离现在的值(前一周传-1,前两周传-2...)

*@returnarray|bool

*/

functionnaturalFormatTime($time_type=1,$prev_num=0){

$today_start_time=strtotime(date('Y-m-d00:00:00',time()));//今天0点的时间戳

if($time_type==1){

if($prev_num==0){

returnarray('start_time'=>$today_start_time,'end_time'=>time(),'show_date'=>date('Y年m月d日',time()));

}elseif($prev_num<0){

$start_time=$today_start_time-86400*abs($prev_num);

$end_time=$start_time+86399;

$show_date=date('Y年m月d日',$start_time);

returnarray('start_time'=>$start_time,'end_time'=>$end_time,'show_date'=>$show_date);

}else{

returnfalse;

}

}elseif($time_type==2){

$today_week=date('w',$today_start_time);

if($today_week==0){

$today_week_start_time=$today_start_time-86400*6;

}else{

$today_week_start_time=$today_start_time-86400*($today_week-1);

}

if($prev_num==0){

$show_date=date('Y年m月d日',$today_week_start_time);

$show_date.='至'.date('d日',time());

returnarray('start_time'=>$today_week_start_time,'end_time'=>time(),'show_date'=>$show_date);

}elseif($prev_num<0){

$start_time=$today_week_start_time-86400*7*abs($prev_num);

$end_time=$start_time+(86400*7-1);

$show_date=date('Y年m月d日',$start_time);

$show_date.='至'.date('d日',$end_time);

returnarray('start_time'=>$start_time,'end_time'=>$end_time,'show_date'=>$show_date);

}else{

returnfalse;

}

}elseif($time_type==3){

if($prev_num==0){

$today_day=ltrim(date('d',$today_start_time),0);

$today_month_start_time=$today_start_time-86400*($today_day-1);

$show_date=date('Y年m月d日',$today_month_start_time);

$show_date.='至'.date('d日',time());

returnarray('start_time'=>$today_month_start_time,'end_time'=>time(),'show_date'=>$show_date);

}elseif($prev_num<0){

$start_time=strtotime(date('Y-m-01',strtotime("$prev_nummonth")));

$days=date('t',$start_time);

$end_time=$start_time+86400*$days-1;

$show_date=date('Y年m月d日',$start_time);

$show_date.='至'.date('d日',$end_time);

returnarray('start_time'=>$start_time,'end_time'=>$end_time,'show_date'=>$show_date);

}else{

returnfalse;

}

}else{

returnfalse;

}

}

/**

*计算近一周或近一个月的开始时间戳和结束时间戳

*@param$type1表示今天,2表示近一周,3表示近一个月

*@returnarray

*/

functionnearFormatTime($type){

$start_time=strtotime(date('Y-m-d00:00:00'));//今天0点的时间戳

$end_time=$start_time+86399;//今天23:59的时间戳

$res=array('start_time'=>0,'end_time'=>$end_time);

if($type==1){

//今天

$res['start_time']=$start_time;

}elseif($type==2){

//近一周

$res['start_time']=$start_time-86400*6;//包括今天,共七天

}elseif($type==3){

//近一个月

$res['start_time']=$start_time-86400*30;//包括今天,共31天

}

return$res;

}

版权声明:本内容来源于网络,如有侵犯您的版权,请联系站长,本站收到您的信息后将及时处理。
上一篇:php将csv文件导入到mysql数据库的方法

 

下一篇:php substr()去掉最后一位字符的实例方法

发布日期:2023/2/23
手机扫二维码直达本页
发布时间:13:35:53
点  击:9
录  入:齐天大圣
相关文章
Baidu
YiJiaCMS 7.3.8 build231228(MSSQL) 闽ICP备05000814号-1
本空间由腾讯云(轻量应用服务器)提供,Cloudflare提供加速防护
运行时间载入中.....