GVKun编程网logo

6月的PHP strtotime将于7月返回(php5.6发布时间)

12

这篇文章主要围绕6月的PHPstrtotime将于7月返回和php5.6发布时间展开,旨在为您提供一份详细的参考资料。我们将全面介绍6月的PHPstrtotime将于7月返回的优缺点,解答php5.6

这篇文章主要围绕6月的PHP strtotime将于7月返回php5.6发布时间展开,旨在为您提供一份详细的参考资料。我们将全面介绍6月的PHP strtotime将于7月返回的优缺点,解答php5.6发布时间的相关问题,同时也会为您带来PHP strtotime +1个月的行为、PHP strtotime(''2039-01-01'') 返回空、PHP strtotime()没有返回正确的月份、php strtotime,mktime,DateTime函数处理时间累加问题的实用方法。

本文目录一览:

6月的PHP strtotime将于7月返回(php5.6发布时间)

6月的PHP strtotime将于7月返回(php5.6发布时间)

我很难过为什么下面的 PHP strtotime函数返回’07’作为月号,而不是$06’当$monthToGet =’June’时:
$monthToGet = $_GET['mon'];
$monthAsNumber = date('m',strtotime($monthToGet));

从搜索开始,它似乎可能是由于我没有指定默认日期参数(在这种情况下是日期和年份).那会是原因吗?

任何建议赞赏!

TL; DR

你是对的

echo date("m",strtotime("June"));
-> 07

但是,这确实有效:

echo date("m",strtotime("1. June 2012"));
-> 06

问题解释了

今天是2012年7月31日,由于您只提供了一个月,因此当前日期和当前年份用于创建有效日期.

见documentation:

NOTE

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC),relative to the timestamp given in Now,or the current time if Now is not supplied.

备择方案

您可以使用date_parse_from_format()strptime()以稍微不同的方法实现您想要的效果.

(感谢johannes_和johann__的输入)

PHP strtotime +1个月的行为

PHP strtotime +1个月的行为

我知道PHP函数的有害行为

时间

例如,向以下日期添加一个月(+1个月):31.01.2011-> 03.03.2011

知道这不是正式的
PHP错误
,并且该解决方案背后有一些争论,但至少对我而言,这种行为造成了很多时间的浪费(过去和现在),我个人讨厌它。


我发现甚至更陌生的是,例如:

MySQL: DATE_ADD(‘2011-01-31’,INTERVAL 1 MONTH)返回2011-02-28或

C# 其中新的DateTime(2011,01,31).AddMonths(1); 将返回28.02.2011

wolframalpha.com给出31.01.2013+ 1 month输入;将于2013年2月28日星期四返回

在我看来,对于 这个我在PHP错误报告中看到 的愚蠢问题,其他人找到了一个更体面的解决方案:
“如果我说我们从现在开始一个月后会见,那将是星期几”或类似的东西。答案是:如果下个月不存在31,请告诉我该月的最后一天,但 请坚持下个月。


所以我的问题是: 是否有一个PHP函数(由某人编写)可以解决这个未被官方认可的bug?因为我不认为我是唯一一个在添加/减去月份时想要其他行为的人。

我对解决方案特别感兴趣,该解决方案不仅可以在本月底使用,而且可以完全替代strtotime。案件strotime +n months也应处理。

编码愉快!

答案1

小编典典

这是您可以使用的算法。实现自己应该足够简单。

  • 在变量中有原始日期和+1个月日期
  • 提取两个变量的月份部分
  • 如果差异大于1个月(或者原始日期是12月,另一个不是1月),则将后一个变量更改为下个月的最后一天。您可以使用例如tdate()得到最后一天:date( ''t.m.Y'' )

PHP strtotime(''2039-01-01'') 返回空

PHP strtotime(''2039-01-01'') 返回空

PHP strtotime(''2039-01-01'') 返回空
strtotime 返回的是int类型,所以转2039年的,返回空。
gmmktime mktime 也都返回int类型。
有没有别的替换方式?

回复内容:

PHP strtotime(''2039-01-01'') 返回空
strtotime 返回的是int类型,所以转2039年的,返回空。
gmmktime mktime 也都返回int类型。
有没有别的替换方式?

5.5.26 正常,你的版本是多少。
有效的时间戳通常从 Fri, 13 Dec 1901 20:45:54 GMT 到 Tue, 19 Jan 2038 03:14:07 GMT
http://php.net/manual/zh/function.strtotime.php

gmmktime和mktime转换正常?你换个php版本吧,操作系统和php版本都要64位的哦

PHP strtotime()没有返回正确的月份

PHP strtotime()没有返回正确的月份

由于当前月/年是2012年1月,为什么以下代码将在2011年12月而不是2011年11月返回?

echo date("F Y",strtotime("-2 months"));

这是在PHP 5.3.0上,如果它有所作为.

解决方法

要获得您正在寻找的内容,您可以使用这个相当冗长的版本:

echo date("F Y",strtotime("first day of this month - 2 months"));

这里详细介绍了原始版本的问题:http://derickrethans.nl/obtaining-the-next-month-in-php.html.引用如下:

Over and over again PHP users complain that next month in PHP’s
date-string parser doesn’t go to the next month,but instead skips to
the one after next month; like in the following example:

06001

The output of the little script will be march. march obvIoUsly doesn’t
follow January as February is in between. However,the current
behavior is correct. The following happens internally:

next month increases the month number (originally 1) by one. This
makes the date 2010-02-31. The second month (February) only has 28
days in 2010,so PHP auto-corrects this by just continuing to count
days from February 1st. You then end up at march 3rd. The formatting
strips off the year and day,resulting in the output march. This can
easily be seen when echoing the date with a full date format,which
will output march 3rd,2010:

这是为了增加月份,但在减去月份时反向相同;没有11月31日,所以strtotime方法“纠正”它到12月1日.

php strtotime,mktime,DateTime函数处理时间累加问题

php strtotime,mktime,DateTime函数处理时间累加问题

时间戳(年月日时分秒)  使用strtotime函数,结合+1 month,-1 month,next month,last month的时候会出现一些问题.

 

demo示例:

1 //时间"2018-10-16 12:00:00"增加一个月
2 $timeOne = strtotime("2018-10-16 12:00:00");
3 echo date("Y-m-d H:i:s", strtotime("+ 1 month", $timeOne ));//2018-10-16 12:00:00[正确]
4  
5 //时间"2018-10-31 10:00:00"增加一个月
6 $timeTwo = strtotime("2018-10-31 12:00:00");
7 echo date("Y-m-d H:i:s", strtotime("+ 1 month", $timeTwo ));//2018-12-01 12:00:00[错误]

 

"date"内部逻辑的处理,其实是正确. 解析内部处理的逻辑?

1 (1): 先做+1 month,假设当前时间为10-31, 增加一个月时间为11-31
2 (2): 再做日期规范化,因为11月没有31日,所以就好像3点60等于4点一样;11月31日就等于12月1日

 

解决方法一: PHP>=5.3  date新增修正短语,用来明确这个问题,那就是"first day of" 和 "last day of",可以限定不要让date自动"规范化"

1 echo date("Y-m-d H:i:s", strtotime("last day of +1 month", strtotime("2018-10-31 12:00:00")));//2018-11-30 12:00:00[正确]

 

解决方法二: mktime  获取当前时间: date("Y-m-d H:i:s", mktime(date(''H''),date(''i'') ,date(''s''), date(''m''), date(''d''), date(''Y'')))

1 //设定时间"2018-10-31 12:00:00"
2 echo date("Y-m-d H:i:s", mktime(date(''12''),0, 0, date(''10''), date(''31''), date(''2018'')));
3 
4 //指定增加30天,时间"2018-11-30 12:00:00"
5 echo date("Y-m-d H:i:s", mktime(date(''12''),0, 0, date(''10''), date(''31'')+30, date(''2018'')));//2018-11-30 12:00:00[正确]

 

解决方法三: (PHP 5 >= 5.2.0, PHP 7)  DateTime

 1 <?php
 2 //设定初始时间: ''Y-m-d H:i:s''
 3 $dateTime = new DateTime("2018-10-31 12:00:00");
 4 
 5 $addMonth = 1; //设定间隔的月份
 6 
 7 //初始时间的天数值
 8 $initDay = $dateTime->format("d");   
 9 
10 //DateInterval构造函数的参数是一个表示时间间隔约定的字符串,这个时间间隔约定以字母P开头,后面跟着一个整数,最后是一个周期标识符,限定前面的整数.
11 //有效周期标识符如下: Y(年) M(月) D(日) W(周) H(时) M(分) S(秒) 间隔约定中既可以有时间也可以有日期,如果有时间需要在日期和时间之间加上字母"T"用来分隔.
12 //例如: 间隔约定P2D表示间隔两天; 间隔约定P2DT5H2M表示间隔两天五小时两分钟
13 
14 $dateTime->add(new DateInterval("P" . $addMonth . "M"));//增加 一个月时间;  2018-12-01 12:00:00[错误]
15 
16 $newDay = $dateTime->format("d");//累加后新的日期的天数值
17 
18 //判断间隔月份,天数值是否相同
19 if($initDay != $newDay) {
20     //不同减去差值
21     $dateTime->sub(new DateInterval("P" . $newDay . "D"));//减去 新初时间,天数差的值
22 }
23 
24 echo $initDay;//31(天)
25 echo "<br>";
26 echo $newDay;//1(天)
27 echo "<br>";
28 echo $dateTime->format("Y-m-d H:i:s");//2018-11-30 12:00:00[正确]

 

我们今天的关于6月的PHP strtotime将于7月返回php5.6发布时间的分享已经告一段落,感谢您的关注,如果您想了解更多关于PHP strtotime +1个月的行为、PHP strtotime(''2039-01-01'') 返回空、PHP strtotime()没有返回正确的月份、php strtotime,mktime,DateTime函数处理时间累加问题的相关信息,请在本站查询。

本文标签: