, 都可能会有这个迷惑, 我们也可以很轻松的验证类似的其他月份, 印证这个结论: var_dump(date("Y-m-d", strtotime("-1 month", strtotime("2017 -03-31"))));//输出2017-03-03var_dump(date("Y-m-d", strtotime("+1 month", strtotime("2017-08-31"))));//输出 (date("Y-m-d", strtotime("last month", strtotime("2017-03-31"))));//输出2017-03-03 那怎么办呢? day of +1 month", strtotime("2017-08-31"))));////输出2017-09-01var_dump(date("Y-m-d", strtotime("first day of next month", strtotime("2017-01-31"))));////输出2017-02-01var_dump(date("Y-m-d", strtotime("last
$t = strtotime("-{$n} days 00:00:00");//当前日期的前n天的0点 $t = strtotime("-{$n} days 23:00:00");//当前日期的前n天的 strtotime("+{$n} days 00:00:00");//当前日期的后n天的0点 $t = strtotime("+{$n} days 23:00:00");//当前日期的后n天的23点 获取指定日期前 $n*86400 s的时间 $t = strtotime("+{$n} days {$data} 00:00:00");//当前日期的后n天的0点 $t = strtotime("+{$n} days "); $t = strtotime("yesterday 00:00:00"); //以上三个结果一样,但是为了严谨期间,如果需要时分秒,给出精确时间 $t = strtotime("tomorrow "); $t = strtotime("tomorrow midnight"); $t = strtotime("tomorrow 00:00:00"); $year = 2015; $month =
var_dump(date('Y-m-d')); var_dump(date('Y-m-d', strtotime('- 1 day'))); var_dump(date('Y-m-d', strtotime ('+ 2 day'))); var_dump(date('Y-m-d', strtotime('- 1 week'))); var_dump(date('Y-m-d', strtotime('+ 2 02" string(10) "2018-10-23" 上面的这些都没有问题,毕竟day和week的时间是固定的,但是month就不一样了,有大月和小月 var_dump(date("Y-m-d", strtotime ("-1 month", strtotime("2018-05-31")))); 打印出来的结果是: string(10) "2018-05-01" !!! ("last day of -1 month", strtotime("2018-05-31")))); 打印结果是: string(10) "2017-04-30" 为了避免 strtotime 引起的问题
():把字符串类型日期格式转成时间戳 使用函数strtotime(),打印前一天日期,参数:String类型 “-1 day” echo date("Y-m-d H:i:s",strtotime("-1day "));输出 2016-05-12 15:27:33 使用函数strtotime(),打印明天日期,参数:String类型 “+1 day” echo date("Y-m-d H:i:s",strtotime s",strtotime("+1 week"));;输出 2016-05-20 15:29:35 使用函数strtotime(),打印下一个月日期,参数:String类型 “+1 month” echo date("Y-m-d H:i:s",strtotime("+1 month")); 输出:2016-06-13 15:37:42 使用函数strtotime(),打印下周一日期,参数:String类型 “last Mondy” echo date("Y-m-d H:i:s",strtotime("next Monday")); 输出:2016-05-16 00:00:00 使用函数strtotime
###获取今日0点的时间戳 $today = strtotime(date('Ymd')); $today2 = strtotime('today'); echo "today = ". $today2; 输出: today = 1463500800 today2 = 1463500800 由此可见,获取今日0点时的时间戳可以直接使用strtotime("today").
今天遇到一个BUG,在使用strtotime(date('Y-m-d') . ' 00:00:00') 获取当天零点时间戳会出现不准确的问题,有时候获取的是正常的零点时间戳,有时候获取的是当天8点的时间戳 解决方案: strtotime(date('Y-m-d')) // 获取当天零点时间戳 strtotime(date('Y-m-d') . ' + 1 day') - 1 // 获取当天23点59分59 秒时间戳 strtotime(date('Y-m-d')) - 1 // 获取昨天23点59分59秒时间戳
Y-m-d H:i:s', 1669043745); // 2022-11-21 23:15:45 如果我们在 WordPress 也这么使用,将会输出:2022-11-21 15:15:45,将会相差8小时 因为 WordPress 为了防止因为系统不同设置造成的问题,先把默认的时区设置为 UTC,也就是格林威治标准时间,和北京时间正好差8个小时。 strtotime("2022-11-21 23:15:45"); // 1669072545 和上面输入的 1669043745 也是相差 8x3600,也是8小时,同样的原因,这个函数也是基于系统默认的时区的 进行日期和时间转换成时间戳操作的时候,把 strtotime 函数替换成 wpjam_strtotime 函数即可。 将日期和时间转换成时间戳的时候:把 strtotime 函数替换成 wpjam_strtotime 函数。
elseif ($weekday == 0) { //1号是周日 $firstWeekDay = 1; } else { $firstWeekDay = 8 $days = 8-$week; $firstWeekendDay = date('Y-m-d H:i:s',strtotime($year ($yearFirstDay)); $days = 8 - $week; $firstWeekendDay = date('Y-m-d H:i:s', strtotime($year elseif ($weekday == 0) { //1号是周日 $firstWeekDay = 1; } else { $firstWeekDay = 8 ) $days = 8-$week; $firstWeekendDay = date('Y-m-d H:i:s',strtotime($year
本文中所有的示例都基于中国时间,即GTM+8 固化在文件 再php.ini配置文件里找到被注释掉的 ;data.timezone = 改为: date.timezone = PRC 其中PRC可以换为 echo strtotime("2014-03-27"), "\n"; echo strtotime("December 31"), "\n"; echo strtotime("now"), "\n"; echo strtotime("10 September 2000"), "\n"; echo strtotime("+1 day"), "\n"; echo strtotime("+1 week") :i:s', time()); echo strtotime($time.'1day'), "\n"; echo "\n"; echo strtotime($time 2] => 12 [3] => 27 [4] => 2 [5] => 114 [6] => 4 [7] => 85 [8] => 0 ) 如果第二参数为真
id=343578′ scrolling=’no’>function get_weekend_days($start_date,$end_date){ if (strtotime($start_date ) > strtotime($end_date)) list($start_date, $end_date) = array($end_date, $start_date); $start_reduce = $end_add = 0; $start_N = date(‘N’,strtotime($start_date)); $start_reduce = ($start_N == 7) ? 2 : 1; $days = abs(strtotime($end_date) – strtotime($start_date))/86400 + 1; return floor(($days + end_N) / 7) * 2 – $start_reduce + $end_add; } echo get_weekend_days(date(“Y-m-d H:i:s”,time()),”2011-8-
本月,上个月,本年 起始时间戳或日期,最新更新时间为2020-04-01 date_default_timezone_set("Asia/Shanghai"); //设置为上海时间 否则开始时间会相差8个小时 (date('Y-m-01 00:00:00',strtotime('-1 month'))); //$end_time = strtotime(date("Y-m-d 23:59:59", strtotime day'))); $begin_year = strtotime(date("Y",time())."-1". "-1"); //本年开始 $end_year = strtotime(date("Y",time())."-12". "-31"); //本年结束 //现在的时间到第二天凌晨相差的时间戳 $time = (strtotime(date('Y-m-d'))+3600*24) - time() ;
",strtotime("+1 day")),"
";
echo "一周后:",date("Y-m-d",strtotime("+1 week")),"
";
echo "一周零两天四小时两秒后 ",strtotime("next Thursday")),"
";
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))." >
//strtotime可以接受第二个参数,类型timestamp,为指定日期
echo date('Y-m-d', strtotime ("+1 day", strtotime('2011-11-01 echo date('Y-m-d',strtotime('+1 d',strtotime('2009-07-08')));//日期天数相加函数
echo date("Y-m-d",'1246982400 ((int)substr("$d",0,4));//取得年份
$month=((int)substr("$d",5,2));//取得月份
$day=((int)substr("$d",8,2
",strtotime("+1 day")),"\n"; echo "一周后:",date("Y-m-d",strtotime("+1 week")),"\n"; echo "一周零两天四小时两秒后 ",strtotime("next Thursday")),"\n"; echo "上个周一:".date("Y-m-d",strtotime("last Monday"))." H:i:s', strtotime ("+1 day", strtotime('2017-11-11'))), "\n"; // 这里+1 day 可以修改参数1为任何想需要的数 ('+1 day', strtotime('2016-09-12'))), "\n\n"; echo strtotime('2016-09-12'), "\n"; echo date("Y-m-d substr("$d",0,4)); //取得年份 $month=((int)substr("$d",5,2)); //取得月份 $day=((int)substr("$d",8,2
示例:echo time(); // 输出当前时间戳二、格式化日期和时间字符串strtotime(): 将日期字符串转换为时间戳 strtotime()函数用于将一个日期字符串转换为Unix时间戳。 示例:$date1 = strtotime('2023-04-26');$date2 = strtotime('2023-05-01');echo ($date2 - $date1)/(60*60*24 示例:$date1 = strtotime('2023-04-26');$date2 = strtotime('2023-05-01');if ($date1 < $date2) { echo ' 示例:echo date_sunrise(strtotime('2023-04-26'), SUNFUNCS_RET_STRING, 31.23, 121.47, 90, 8); // 输出:05:12echo date_sunset(strtotime('2023-04-26'), SUNFUNCS_RET_STRING, 31.23, 121.47, 90, 8); // 输出:18:10
strtotime : strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳
substr(string,start,length):
string : 必需。 天
"; $hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600); echo "相差小时数:". 小时
"; $minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60); echo"相差分钟数:". 分钟
"; $second=floor((strtotime($enddate)-strtotime($startdate))%86400%60); echo"相差秒数:". date('i');
foreach ($array as $key => $value) {
$d = substr($value['create_time'], 8,
$Y = 2016;//获取年,示例,真实环境从前端获取数据 $m = 8;//获取月,示例,真实环境从前端获取数据 $month = $Y."-". $m;//当前年月 $month_start = strtotime($month);//指定月份月初时间戳 $month_end = mktime(23, 59, 59, date('m', strtotime($month))+1, 00);//指定月份月末时间戳 //dump(array("month"=>$month,"month_start"=>$month_start,"month_end "=>$month_end));//输出 当天的开始和结束 $current=date("y-m-d",time()); $sign['startime']=strtotime ($current); $sign['endtime']=mktime(23, 59, 59, date('m-d', strtotime($current))+1, 00);
** 3 * Created by PhpStorm. 4 * Author: 林冠宏 5 * Date: 2016/6/4 6 * Time: 16:06 7 * 8 GetTimeRang($timeType = null,$selectTime = null){ 59 header("content-type: text/html;charset=utf-8" (date("Y-m-d",time())); 93 }else{ 94 $timeF = strtotime($selectTime); 95 )); // 获取传过来的月所有天数 152 $timeF = strtotime($selectTime); 153 } 154 $timeL "-1-1"); 171 $year = (int)$selectTime + 1; 172 } 173 $timeL = strtotime($
{ foreach ($this->holidayDay as $startDate => $endDate) { $this->holidayData[strtotime ($startDate)] = strtotime($endDate); } ksort($this->holidayData); foreach ($this ->holidayExtraWorkDay as $startDate => $endDate) { $this->workDayData [strtotime($startDate ) { //判断当日是否为上班日 $isWorkDay = $this->checkIsWorkDay($datetime); $time = strtotime $startTime)] = strtotime($date . " " .
UserModel; $user->name = 'fs哆哆'; $user->email = 'yg@qq.com'; $user->birthday =strtotime user = new UserModel(); $list = [ ['name'=>'小龙女','email'=>'xlv@qq.com','birthday'=>strtotime ('2019-2-1')], ['name'=>'黄蓉','email'=>'hr@qq.com','birthday'=>strtotime('2018-2-1')], , ['id' => 7, 'name' => '任我行', 'email' => 'rwx@qq.com'], ['id' => 8,
php header("content-type:text/html;charset=utf-8"); date_default_timezone_set('PRC'); function $day7; $weekarray=array("日","一","二","三","四","五","六"); $week1=$weekarray[date("w",strtotime("$date1")) ]; $week2=$weekarray[date("w",strtotime("$date2"))]; $week3=$weekarray[date("w",strtotime("$date3"))] ; $week4=$weekarray[date("w",strtotime("$date4"))]; $week5=$weekarray[date("w",strtotime("$date5"))]; $week6=$weekarray[date("w",strtotime("$date6"))]; $week7=$weekarray[date("w",strtotime("$date7"))];