先做-1 month, 那么当前是07-31, 减去一以后就是06-31. 2. 再做日期规范化, 因为6月没有31号, 所以就好像2点60等于3点一样, 6月31就等于了7月1 是不是逻辑很”清晰”呢? , 都可能会有这个迷惑, 我们也可以很轻松的验证类似的其他月份, 印证这个结论: 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 那怎么办呢?
非常直观的一些用用法 获取相比当前时间前N天的时间戳 $n = 2;//这个取值可以随意改变 $t = strtotime("-{$n} days");//相比于当前时间戳减去 $n*86400 s的时间 $t = strtotime("-{$n} days 00:00:00");//当前日期的前n天的0点 $t = strtotime("-{$n} days 23:00:00");//当前日期的前n天的 23点 获取相比当前时间后N天的时间戳 $n = 2;//这个取值可以随意改变 $t = strtotime("+{$n} days");//相比于当前时间戳加上 $n*86400 s的时间 $t = /后N天的时间戳 $n = 2;//这个取值可以随意改变 $date = "2015-02-27"; $t = strtotime("+{$n} days {$date}");//相比于当前时间戳加上 2; $t = strtotime("first day of {$year}-{$month}");//指定月份的第一天 $t = strtotime("first day of {$year}-{
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 “last Mondy” echo date("Y-m-d H:i:s",strtotime("next Monday")); 输出:2016-05-16 00:00:00 使用函数strtotime (),打印下周零两天两小时两秒后日期,参数:String类型组合一下 echo date("Y-m-d H:i:s",strtotime("+1 week 2 day 2 hour")); 输出 2016
###获取今日0点的时间戳 $today = strtotime(date('Ymd')); $today2 = strtotime('today'); echo "today = ". $today; echo "today2 = " . $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秒时间戳
int strtotime ( string $time [, int $now = time() ] ) 参数1:日期/时间字符串。 参数2:用来计算返回值的时间戳。 , "\n"; echo strtotime("Saturday"), "\n"; echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n"; echo 他用来描述一段时间,形如P1Y2M3DT4H5M6S,字母都是大写。其中P后面跟年月日,T后面是时分秒。 2] => 12 [3] => 27 [4] => 2 [5] => 114 [6] => 4 [7] => 85 [8] => 0 ) 如果第二参数为真 例: echo cal_days_in_month(CAL_GREGORIAN, 2, 2014); //28 比之前mktime()要简单的多吧。
{ 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 ($date)); if (in_array($week, [1, 2, 3, 4, 5])) {//周一到周五 //验证是否为节假日,如果是则不用上班 $startTime)] = strtotime($date . " " .
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 + $start_N – 1 – $end_N) / 7) * 2 – $start_reduce + $end_add; } echo get_weekend_days(date(“Y-m-d H:i:
$days.' 23:59:59'); //组装一年中 完整的各周开始和结束时间戳 $days2 = ((strtotime($yearEndDay)-$weeks[1]['end']) /86400)/7; $days3 = (int)floor($days2); $length = $days3+1; for($i=2;$i<=$length;$i++) { $days . ' 23:59:59'); //各周开始和结束时间戳 $days2 = ((strtotime($yearEndDay) - $weeks[1]['week_end']) / 86400) / 7; $days3 = (int)floor($days2); $length = $days3 + 1; for ($i = 2; $i <= $length $days.' 23:59:59'); //组装一年中 完整的各周开始和结束时间戳 $days2 = ((strtotime($yearEndDay)-$weeks[1]['week_end'])/86400
:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "
";
echo "下个星期四:",date("Y-m-d >
//strtotime可以接受第二个参数,类型timestamp,为指定日期
echo date('Y-m-d', strtotime ("+1 day", strtotime('2011-11-01 _1="2009-07-08";
echo $Date_1+1;
$Date_2="2009-06-08";
$Date_List_a1=explode("-",$Date_1);
$Date_List_a2 =explode("-",$Date_2);
$d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]);
$d2=mktime (0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]);
$Days=round(($d1-$d2)/3600/24);
echo "两日期之前相差有
Y-m-d", mktime(23, 59, 59, date("m") - ($month - 1), 0, date("Y"))); // 以下方法有误差,例如3月31日那天的上个月时间匹配出是3月2日 ,已亲自测试,所以已废弃 //$begin_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month'))); //$end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').' day'))); $begin_year = strtotime(date("Y",time())."-1". "-1"); //本年开始 $end_year = strtotime(date("Y",time())."-12".
$day = '2014-03-28'; $d1 = date("Y-m-d", strtotime("0 day $day"));//第1天03-28 $d3 = date("Y-m-d", strtotime("2 day $day"));//第3天 $d7 = date("Y-m-d", strtotime("6 day $day"));//第7天 $d15 = date("Y-m-d ", strtotime("14 day $day"));//第15天 $d30 = date("Y-m-d", strtotime("29 day $day"));//第30天 $rd5 = date ("Y-m-d", strtotime("-4 day $day"));//最后5天 $rd3 = date("Y-m-d", strtotime("-2 day $day"));//最后3天 $rd2 = date("Y-m-d", strtotime("-1 day $day"));//最后2天 $rd1 = date("Y-m-d", strtotime("-0 day $day")
:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "\n"; echo "下个星期四:",date("Y-m-d ",strtotime("next Thursday")),"\n"; echo "上个周一:".date("Y-m-d",strtotime("last Monday"))." "$d",0,4)); //取得年份 $month=((int)substr("$d",5,2)); //取得月份 $day=((int)substr("$d",8,2)) =explode("-",$date_2); $d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]); $d2 =mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]); $Days=round(($d1-$d2)/3600/24);
示例:echo time(); // 输出当前时间戳二、格式化日期和时间字符串strtotime(): 将日期字符串转换为时间戳 strtotime()函数用于将一个日期字符串转换为Unix时间戳。 示例:$date1 = strtotime('2023-04-26');$date2 = strtotime('2023-05-01');echo ($date2 - $date1)/(60*60*24 示例:$date1 = date_create('2023-04-26');$date2 = date_create('2023-05-01');$interval = date_diff($date1 , $date2);echo $interval->format('%R%a days'); // 输出:+5 daysstrtotime(): 比较两个日期的大小 strtotime()函数可以用于比较两个日期的大小 示例:$date1 = strtotime('2023-04-26');$date2 = strtotime('2023-05-01');if ($date1 < $date2) { echo '
strtotime : strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳 substr(string,start,length): string : 必需。 i'); foreach ($array as $key => $value) { $d = substr($value['create_time'], 8, 2) ; //获取创建日期:单位:日 $h = substr($value['create_time'], 11, 2); //获取发布创建日期:单位:时 $m = substr($value['create_time'], 14, 2); //获取发布创建日期:单位:时 //获取当前时间与发布时间的相差的天数 break; } break; case $date < 2:
wpjam_strtotime 前面解决了在 WordPress 中基于时间戳正确显示时间的问题,然后还有一个常见的操作就是将日期转换为日期戳,PHP 提供了一个 strtotime 的函数: echo 所以为了方便将当地时间正确转换成时间戳,所以我写了一个函数 wpjam_strtotime: function wpjam_strtotime($string){ return date_create ("2022-11-21 23:15:45"); // 1669043745 wpjam_strtotime 很快会在 WPJAM Basic 新版本中发布,所以有了第二条规则:在 WordPress 进行日期和时间转换成时间戳操作的时候,把 strtotime 函数替换成 wpjam_strtotime 函数即可。 将日期和时间转换成时间戳的时候:把 strtotime 函数替换成 wpjam_strtotime 函数。
将年月日时间转换成时间戳 echo strtotime('2020-01-15 11:20:10'); //直接输出转换后的时间戳 当前时间加七天 echo date("Y-m-d H:i:s",strtotime ("+7 day")); //输出七天后的日期 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获取一周零两天四小时五分钟两秒后时间 date("Y-m-d H:i:s",strtotime("+1 week 2 days 4 hours 5 minute 2 seconds")); php
php字符时间转换的方法:1、通过php中的“strtotime()”函数将任何英文文本的日期时间描述解析为时间戳;2、使用php中的“mktime()”函数从日期取得时间戳即可。 strtotime() strtotime() 函数用于将英文文本字符串表示的日期转换为时间戳,为 date() 的反函数,成功返回时间戳,否则返回 FALSE 。 > 自定义函数 下面的函数与strtotime功能差不多。<? str_format_time(date_str); function str_format_time($timestamp = ”) { if (preg_match(“/[0-9]{4}-[0-9]{1,2} -[0-9]{1,2} (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])/i”, $timestamp)) { list(date,time)=explode
用途 功能及事项 使用方法 代码及注释 前序: 总体来说,我更应该是一个 android 移动开发者,而不是一个 phper,如果说只做移动端的 APP ,我也不会学这么多,这 2年来 功能及事项: 1,使用后产生的是,要查找的时间范围,根据传入参数选择生产,也可以组合 sql 语句返回,本例就是; 2,已实现: 1) 按 日 生成范围 2)按周 生成范围 php 2 /** 3 * Created by PhpStorm. 4 * Author: 林冠宏 5 * Date: 2016/6/4 6 * Time: 16:06 * 8 * 前序: 9 * 总体来说,我更应该是一个 android 移动开发者,而不是一个 phper,如果说只做移动端的 APP , 10 * 我也不会学这么多,这么 2年来 Whatever,enjoy this `Class`. 13 * 14 * 功能: 15 * 1,产生 要查找的 时间范围 16 * 2,格式是 时间戳,拥有时间戳