再做日期规范化, 因为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 那怎么办呢? day of +1 month", strtotime("2017-08-31"))));////输出2017-09-01var_dump(date("Y-m-d", strtotime("first
$t = strtotime("-{$n} days 00:00:00");//当前日期的前n天的0点 $t = strtotime("-{$n} days 23:00:00");//当前日期的前n天的 $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 = fortnight ago" 两周前 week day hours minute second或者 sec 计算顺序 -ago 从当前时间往前计算 "24 hours ago"(hour也可以) "3
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秒时间戳
echo mktime(28, 0, 0, 3, 27, 1990); //638568000 //1990-3-28 04:00:00 echo mktime(-28, 0, 0, 3, 27, 1990 echo mktime(0, 0, 0, 3, 1, 1990); //1990-3-01 00:00:00 echo mktime(0, 0, 0, 3, 0, 1990); //636134400 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") 他用来描述一段时间,形如P1Y2M3DT4H5M6S,字母都是大写。其中P后面跟年月日,T后面是时分秒。
{ 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 . " " .
59', strtotime("$startstr +6 day"))); $Monthend = strtotime(date('Y-m-d 23:59:59', strtotime("$startstr /86400)/7; $days3 = (int)floor($days2); $length = $days3+1; for($i=2;$i<=$length;$i++) { / 86400) / 7; $days3 = (int)floor($days2); $length = $days3 + 1; for ($i = 2; $i <= $length 59', strtotime("$startstr +6 day"))); $Monthend = strtotime(date('Y-m-d 23:59:59', strtotime("$startstr )/7; $days3 = (int)floor($days2); $length = $days3+1; for($i=2;$i<=$length;$i++) { $weeks[$i]['week_start
$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")
$end_time = date("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".
格式参数在这里简单介绍几个:
一些常用于日期的字符:
Y - 完整表示年份(四位数字:2019)
y - 表示年份(两位数字:19)
F - 表示月份(完整的文本格式: January 或者 March)
M - 表示月份(3个字母 表示月份,有前导0(数字:04)
n - 表示月份,无前导0(数字:4)
d - 表示月份中的第几天,有前导0(01-31)
j - 表示月份中的第几天,无前导0(1-31)
D - 表示星期几(3字母 语法:
strtotime(time,now)
通过 strtotime() 函数创建日期和时间:
实例:
<? "
";
$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "
";
? >
声明:本文由w3h5原创,转载请注明出处:《PHP获取当前时间、年份、月份、日期和天数》 https://www.w3h5.com/post/268.html
获取前一天的时间: $mytime= date("Y-m-d H:i:s", strtotime("-1 day")); 获取三天前的时间: $mytime= date("Y-m-d H:i:s", strtotime("-3 day")); 获取前一个月的时间: $mytime= date("Y-m-d H:i:s", strtotime("-1 month")); 获取前3个月的时间: $mytime= date("Y-m-d H:i:s", strtotime("-3 month")); 获取前一个小时的时间: $mytime= date("Y-m-d H:i:s", strtotime ("-1 hour")); 获取前一年的时间: $mytime= date("Y-m-d H:i:s", strtotime("-1 year")); 发布者:全栈程序员栈长,转载请注明出处:https
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 函数。
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"相差秒数:". $array[$key]['create_time'] = "昨天";
break;
case $date < 3:
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) ? 1 : 0; $end_N = date(‘N’,strtotime($end_date)); in_array($end_N,array(6,7)) && $end_add = ($end_N = 2 : 1; $days = abs(strtotime($end_date) – strtotime($start_date))/86400 + 1; return floor(($days +
本文操作环境:windows7系统、PHP5.6版,DELL G3电脑。 strtotime() strtotime() 函数用于将英文文本字符串表示的日期转换为时间戳,为 date() 的反函数,成功返回时间戳,否则返回 FALSE 。 php $lastday = mktime(0, 0, 0, 3, 0, 2008); echo strftime(“2008年最后一天是:%d”, $lastday); // 2008年最后一天是: > 自定义函数 下面的函数与strtotime功能差不多。<? 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]
,使用后产生的是,要查找的时间范围,根据传入参数选择生产,也可以组合 sql 语句返回,本例就是; 2,已实现: 1) 按 日 生成范围 2)按周 生成范围 3) 按月 生成范围 4)按年 生成范围 3,所用语言是 php,服务器解析需要安装 Apache,或者 Nginx; 4,格式是时间戳,切记,拥有时间戳,就可以任意处理,可以生产这种日期时间格式 : 2016-7-08 12:1:3; 5,常见的使用场景是,根据 时间范围 搜索数据; 6,我会提供一个链接供大家直接点击看 输出效果。 php 2 /** 3 * Created by PhpStorm. 4 * Author: 林冠宏 5 * Date: 2016/6/4 6 * Time: 16:06 Class`. 13 * 14 * 功能: 15 * 1,产生 要查找的 时间范围 16 * 2,格式是 时间戳,拥有时间戳,可以任意处理 17 * 3,
2、获取时间戳方法time()、strtotime() 这两个方法,都可以获取php中unix时间戳,time()为直接获取得到,strtotime(time, now)为将时间格式转为时间戳, 3、 23:00:00(结果同上,只是多了一个时间戳参数)(时间戳转换为日期格式的方法) echo date(‘Y’).’年’.date(‘m’).’月’.date(‘d’).’日’,输出结果:2012年3月 ($time)用法 比如: echo strtotime(’2012-03-22′),输出结果:1332427715(此处结果为随便写的,仅作说明使用) echo strtotime(date(‘Y-d-m H:i:s’,strtotime(‘+1 day’)),输出结果:2012-03-23 23:30:33(会发现输出明天此时的时间) echo date(‘Y-m-d H:i:s’,strtotime ) 等等,自己去变通研究吧,strtotime()方法可以通过英文文本的控制Unix时间戳的显示,而得到需要的时间日期格式。
2、获取时间戳方法time()、strtotime() 这两个方法,都可以获取php中unix时间戳,time()为直接获取得到,strtotime(time, now)为将时间格式转为时间戳, 3、 23:00:00(结果同上,只是多了一个时间戳参数)(时间戳转换为日期格式的方法) echo date(‘Y’).’年’.date(‘m’).’月’.date(‘d’).’日’,输出结果:2012年3月 ($time)用法 比如: echo strtotime(’2012-03-22′),输出结果:1332427715(此处结果为随便写的,仅作说明使用) echo strtotime(date(‘Y-d-m H:i:s’,strtotime(‘+1 day’)),输出结果:2012-03-23 23:30:33(会发现输出明天此时的时间) echo date(‘Y-m-d H:i:s’,strtotime ) 等等,自己去变通研究吧,strtotime()方法可以通过英文文本的控制Unix时间戳的显示,而得到需要的时间日期格式。