企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
| 时间日期转换 | | | --- | --- | | 系统时区 | --- | | date_default_timezone_set() | 设定用于所有日期时间函数的默认时区 | | date_default_timezone_set('PRC') | 默认时区为亚洲 | | date_default_timezone_set('Asia/Shanghai') | 默认时区为中国上海 | | date_default_timezone_get() | 获取当前脚本使用的默认时区 | | 时间日期 | --- | | date("Y-m-d") | 当天日期 | | date("Y-m-d",strtotime("-1 day")) | 昨天的日期 | | date("Y-m-d",strtotime("+1 day")) | 明天的日期 | | date("Y-m-d",strtotime("+1 week")) | 一周后的日期 | | date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")) | 一周后零两天四小时两秒后 | | date("Y-m-d",strtotime("next Thursday")) | 下个星期四 | | date("Y-m-d",strtotime("last Monday")) | 上个周一 | | date("Y-m-d",strtotime("last month")) | 一个月前 | | date("Y-m-d",strtotime("+1 month")) | 一个月后 | | date("Y-m-d",strtotime("+10 year")) | 十年后 | | strtotime(date('Y-m-d').' -7 day') | 7天之前的时间戳 | | strtotime("midnight first day of this month") | 本月第一天 | | strtotime("midnight first day of next month") | 下个月第一天 | | strtotime("last day of next month") | 下个月的最后一天 | | date('Y-m-d H:i:s',strtotime('now')) | 当前时间戳 2017-01-09 21:04:11 | | date('Y-m-d H:i:s',strtotime('+1second')) | 当前时间戳+1秒 2017-01-09 21:04:12 | | date('Y-m-d H:i:s',strtotime('+1minute')) | 当前时间戳+1分 2017-01-09 21:05:11 | | date('Y-m-d H:i:s',strtotime('+1hour')) | 当前时间戳+1小时 2017-01-09 22:04:11 | | date('Y-m-d H:i:s',strtotime('+1day')) | 当前时间戳+1天 2017-01-10 21:04:11 | | date('Y-m-d H:i:s',strtotime('+1week')) | 当前时间戳+1周 2017-01-16 21:04:11 | | date('Y-m-d H:i:s',strtotime('+1month')) | 当前时间戳+1月 2017-02-09 21:04:11 | | date('Y-m-d H:i:s',strtotime('+1year')) | 当前时间戳+1年 2018-01-09 21:04:11 | | date('Y-m-d H:i:s',strtotime('+12year 12month 12day 12hour 12minute 12second')) | 当前时间戳+12年,12月,12天,12小时,12分,12秒 2030-01-22 09:16:23 | | date('Y-m-d H:i:s', strtotime('yesterday')) | 昨天 | | date('Y-m-d H:i:s', strtotime('last week')) | 上周 | | date('Y-m-d H:i:s', strtotime('this week midnight')) | 本周开始时间 | | date('Y-m-d H:i:s', strtotime('first day of this month midnight')) | 本月开始时间 | | date('Y-m-d H:i:s', strtotime('+1 month')) | 1个月后的今天 | | 比较特殊的转换 | --- | | date('Y-m-d H:i:s', strtotime('20160130180001')) | 输出:2016-01-30 18:00:01 | ``` #Example 当前时间加一天或一年,指定时间戳加1月、1周、1小时、1分、1秒原理一样 $time = time(); 1、方法一 date('Y-m-d H:i:s', $time + 1 * 24 * 60 * 60) date('Y-m-d H:i:s', $time + 365 * 24 * 60 * 60) 2、方法二 $date = date('Y-m-d H:i:s', $time) date('Y-m-d H:i:s', strtotime("$date + 1day")) date('Y-m-d H:i:s', strtotime("$date + 1year")) 3、方法三 date('Y-m-d H:i:s', strtotime("+ 1day", $time)) date('Y-m-d H:i:s', strtotime("+ 1year", $time)) 时间戳转日期 date('Y-m-d', time()) 日期转时间戳 strtotime(date('Y-m-d')) //注意,变量$dt有大小限制,??? $dt = date('Y-m-d H:i:s',time()) strtotime("$dtday") ``` | 计算月初月末 | | | --- | --- | ``` #原理:根据时间日期和时间戳的转换 $date = date('Y-m-d H:i:s'); #获取当前的日期 $begin_month = date('Y-m-01', strtotime($date)); #当前的日期只取年月,日为01则月初,时分秒填充零 $end_month = date('Y-m-d', strtotime("$begin_month +1 month -1 day")); #再月初的基础上加一个月,即下个月的月初,再减去一天,即月底 #或通过月底计算下一天时间为下个月初,再减去1秒,即月底23:59:59 $month_end = date('Y-m-d H:i:s', strtotime("next day", strtotime($end_month)) - 1); ``` | 计算当前时间到指定时间经历的月份 | | | --- | --- | ``` $start_time = "2019-01"; $strtotime_start_time = strtotime($start_time); $lastMonth = strtotime(date("Y-m", time())); #当前时间戳 $current_time = strtotime(date('Y-m-01')); $monthArr = []; for ($i = 1; $lastMonth > $strtotime_start_time; $i++) { $t = "-". $i ." month"; $lastMonths = date('Y-m', strtotime($t, $current_time)); #当前日期减i个月 array_push($monthArr, $lastMonths); #插入到数组中 $lastMonth = strtotime($lastMonths); #日期格式转回时间戳 } print_r($monthArr); #输出 Array ( [0] => 2019-05 [1] => 2019-04 [2] => 2019-03 [3] => 2019-02 [4] => 2019-01 ) ``` | 时间超过2038年报错异常处理 | | | --- | --- | ``` 原因:当要转换的日期大于 2038 年 1 月 19 日时报错处理 1、时间戳转日期 $expire_time = time()+10000*24*3600; #2046年 $date_time = new \DateTime('@'.$expire_time); $date_time->setTimezone(new \DateTimeZone('PRC')); echo $date_time->format('Y-m-d H:i:s'); 输出:2046-11-10 10:50:57 2、日期转时间戳 $date = new \DateTime('2047-06-19 00:00:00'); echo $date->format('U'); 输出:2444486400 ``` | date()函数注意事项 | | | --- | --- | ``` windows系统 date()函数在2099年时报错 linux系统 date()正常 ```