php获取今日、昨日、最近7天、最近30天等的时间戳方法

分类栏目:网站代码

493

开发工作中可能会需要做一些统计数据,例如今日、昨日、最近7天、最近30天的订单量,或当前时间(某个时间)的一个月之前、一个月之后的日期等,通常我们也会需要获取到某一天的开始时间戳或结束时间戳。下面以PHP为例直接给出代码,strtotime()、mkdir()为输出时间戳,date()为输出具体日期:

首先说明一下date()函数的格式:
date('Y-m-d',timestamp); //输出年-月-日
date('Y-m-d H:i:s',timestamp); //输出年-月-日 时:分:秒

//php获取今天日期
date("Y-m-d",strtotime("today"));//strtotime(‘today’)输出今天的开始时间戳

date("Y-m-d",time()); //time()输出当前秒时间戳

//php获取昨天日期
date("Y-m-d",strtotime("-1 day")); 或 date("Y-m-d",strtotime("yesterday"));

//php获取明天日期
date("Y-m-d",strtotime("+1 day")); 或 date("Y-m-d",strtotime("tomorrow "));

//php获取7天后日期
date("Y-m-d",strtotime("+7 day"));

//php获取30天后日期
date("Y-m-d",strtotime("+30 day"));

//php获取一周后日期
date("Y-m-d",strtotime("+1 week"));

//php获取一个月后日期
date("Y-m-d",strtotime("+1 month"));

//php获取一个月前日期
date("Y-m-d",strtotime("last month")); 或  date("Y-m-d",strtotime("-1 month"));

//php获取一年后日期
date("Y-m-d",strtotime("+1 year"));

//php获取一周零两天四小时五分钟两秒后时间
date("Y-m-d H:i:s",strtotime("+1 week 2 days 4 hours 5 minute 2 seconds"));

//php获取下个星期四日期
date("Y-m-d",strtotime("next Thursday"));  

//php获取上个周一日期
date("Y-m-d",strtotime("last Monday"));

//php获取今天起止时间戳
mktime(0,0,0,date('m'),date('d'),date('Y'));
mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

//php获取昨天起止时间戳
mktime(0,0,0,date('m'),date('d')-1,date('Y'));
mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

//php获取上周起止时间戳
mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));

//php获取本月起止时间戳
mktime(0,0,0,date('m'),1,date('Y'));
mktime(23,59,59,date('m'),date('t'),date('Y'));