企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
[TOC] >[danger] time() 返回的时间戳是当前时间的秒数 求月份的最大天数 ``` $feb=is_leap($year) ?29:28;//2月 cal_days_in_month(CAL_GREGORIAN,$month,$year);//CAL_GREGORIAN=0 //需要开启扩展 //推荐 $day = date("t",strtotime("2017-02")); ``` ``` date_default_timezone_set('PRC'); $today = getdate(strtotime('2003-06-17 21:58:40')); print_r($today); /*结果: Array ( [seconds] => 40//秒的数字表示`0`到`59` [minutes] => 58//分钟的数字表示`0`到`59` [hours] => 21//小时的数字表示`0`到`23` [mday] => 17//月份中第几天的数字表示`1`到`31` [wday] => 2//星期中第几天的数字表示`0`(周日) 到`6`(周六) [mon] => 6//月份的数字表示 1 到 12 [year] => 2003//4 位数字表示的完整年份 [yday] => 167//一年中第几天的数字表示`0`到`365` [weekday] => Tuesday//星期几的完整文本表示 `Sunday`到`Saturday` [month] => June//月份的完整文本表示,比如 January 或 March [0] => 1055901520//自从 Unix 纪元开始至今的秒数,和[time()](https://www.php.net/manual/zh/function.time.php)的返回值以及用于[date()](https://www.php.net/manual/zh/function.date.php)的值类似 ) */ strtotime返回时间戳 指定日期加一天 $time=strtotime("+1 day",strtotime('2025-12-25')), $time1=date("Y-m-d H:i:s",$time); //当前时间减一天 $time1=date("Y-m-d H:i:s",time()-3600*24); print_r($time1);//2018-04-26 12:02:15 $time2 = date("Y-m-d H:i:s",strtotime("-1 day")); print_r($time2);//2018-04-26 12:02:15 //mktime(时,分,秒,月,日,年);//获得一个日期的时间戳 $today_start= mktime(0,0,0,4,27,2018); $time3 = date("Y-m-d H:i:s",$today_start); print_r($time3);//2018-04-27 00:00:00 // 当天的零点时间戳 $dayBegin = strtotime(date('Y-m-d', time())); // 当天的24点时间戳 $dayEnd = $dayBegin + 24 * 60 * 60; //2016-11-01 00:00:00 $todayStart= date('Y-m-d 00:00:00', time()); //2016-11-01 23:59:59 $todayEnd= date('Y-m-d 23:59:59', time()); ``` ``` 1. 获取上个月第一天及最后一天. 月初 date ('Y-m-01', strtotime ('-1 month')); 月末 date ('Y-m-t', strtotime ('-1 month'));  上个月21号 $start_time =Request::param('order_date_begin')?:date('Y-m-d', strtotime(date('Y-m-21') . " - 1 month"));  下个月5号: $end_time = Request::param('order_date_begin')?:date('Y-m-d', strtotime(date('Y-m-05') . " + 1 month"));  上个月第一天: date ('Y-m-d', strtotime (date ('Y-m-01') . ' -1 month')); // 计算出本月第一天再减一个月  上个月最后一天: date ('Y-m-d', strtotime (date ('Y-m-01') . ' -1 day')); // 计算出本月第一天再减一天  前几个月: $month = date('Ym', strtotime(date('Y-m-01') . " - $i month")); 2. 获取当月第一天及最后一天. 当月月初 date ('Y-m-01', strtotime (date ("Y-m-d"))); 当月月末 date ('Y-m-d', strtotime (date ('Y-m-01', strtotime (date ("Y-m-d"))) . "+1 month -1 day")); 3. 获取当天年份、月份、日及天数. "本月共有:".date ("t"); "当前年份".date ('Y'); "当前月份".date ('m'); "当前几号".date ('d'); 4.获取某一天的日期 echo date("Y-m-d H:i:s",strtotime("-7 day")); echo date("Y-m-d H:i:s",strtotime("7 day")); //一小时之前的时间 echo strtotime("-1 hours"); //五小时之后的时间 echo strtotime("+5 hours"); //一周之后的时间 echo strtotime("+1 week"); //下周一的时间 echo strtotime("next Monday"); //一周的最后一天,就是周日 echo strtotime("last Sunday"); ``` ## 获取两个日期之间的所有日期 ``` function createDateRange($date_from, $date_to, $format = "Y-m-d") { $date_from = strtotime($date_from); $date_to = strtotime($date_to); $dateRange=[]; for ($i=$date_from; $i<=$date_to; $i+=86400) { $dateRange[]= date($format, $i); } return $dateRange; } $all_dates=createDateRange("2015-01-01", "2015-02-05"); //效果 Array ( [0] => 2015-01-01 [1] => 2015-01-02 [2] => 2015-01-03 [3] => 2015-01-04 [.] => ... [34] => 2015-02-04 [35] => 2015-02-05 ) ``` 方法2: ``` /** * Returns every date between two dates as an array * @param string $startDate the start of the date range * @param string $endDate the end of the date range * @param string $format DateTime format, default is Y-m-d * @return array returns every date between $startDate and $endDate, formatted as "Y-m-d" */ function createDateRange($startDate, $endDate, $format = "Y-m-d") { $begin = new DateTime($startDate); $end = new DateTime($endDate);//object(DateTime)#2 (3) { ["date"]=> string(26) "2015-02-05 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } $interval = new DateInterval('P1D'); // 1 Day $dateRange = new DatePeriod($begin, $interval, $end); $range = []; foreach ($dateRange as $date) { $range[] = $date->format($format); } return $range; } $all_dates=createDateRange("2015-01-01", "2015-02-05"); print_r($all_dates); ``` 效果: ``` Array ( [0] => 2015-01-01 [1] => 2015-01-02 [2] => 2015-01-03 [3] => 2015-01-04 [.] => ... [33] => 2015-02-03 [34] => 2015-02-04 ) ``` ## php获取日期是星期几 ``` date("l"); //小写L data就可以获取英文的星期比如Sunday date("w"); //小写W 这个可以获取数字星期比如123,注意0是星期日 ``` **获取指定日期是:** ``` $weekarray=array("日","一","二","三","四","五","六"); echo "星期".$weekarray[date("w",strtotime("2011-11-11"))]; ```