- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在使用 date()
和 strtotime()
函数在下拉列表中显示接下来的 3 个月。
PHP 代码:
echo date("m/Y",strtotime("+0 months"));
echo date("m/Y",strtotime("+1 months"));
echo date("m/Y",strtotime("+2 months"));
但是,如果脚本在服务器日期为 30 日或 31 日时运行,则下个月(即 2 月)将显示为 3 月。即上面的脚本应该返回
01/2012
02/2012
03/2012
但是,它实际上显示的不是那个
01/2012
03/2012
03/2012
那是因为二月没有 30 日或 31 日,所以脚本将“31/02”翻译成“01/03”。
我看过php.net 上的strtotime()
页面,这个问题已经被提出来了,但是没有任何有用的解决方案。那么任何人都可以帮我找到解决这个问题的简单方法吗?提前致谢!
最佳答案
如文档中所述,您应该将当月第一天的日期作为第二个参数传递给 strtotime()
函数:
$base = strtotime(date('Y-m',time()) . '-01 00:00:01');
echo date('m/Y',strtotime('+0 month', $base));
echo date('m/Y',strtotime('+1 month', $base));
echo date('m/Y',strtotime('+2 month', $base));
看看它是否有效:http://ideone.com/eXis9
01/2012
02/2012
03/2012
关于PHP date() 和 strtotime() 在 31 日返回错误的月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9058523/
在 PHP 中对 strtotime() 进行了一番研究之后,我注意到当您传入空格和/或点时,它会返回一个有效的时间戳。 var_dump(strtotime(" ")); var_dump(strt
这是我的代码: $testtime = str_replace('.', '-', '2009.07.08 17:01'); $timestamp = strtotime($testtime); ec
我有一个脚本,它从 excel 表中读取值并将它们插入到 sql 表中。我的问题是当脚本读取某些日期字段时,它会将它们设置为默认值:1970-01-01 01:00:00 我注意到当遇到“天”大于 1
我有 2 台服务器。他们都使用相同的操作系统:Ubuntu 14.04 服务器 + php-fpm。我不能让它们生成相同的 strtotime结果。 日期时间:2013-12-30 18:29:59第
我使用 ths 方法来查找两个时间戳之间的差异并获取这两个时间之间的秒数,然后像计数器一样用 jquery 刷新信息。 $diff = strtotime(date('Y-m-d H:i:s')) -
我正在构建一个日记,其中包含每周 View ,我认为我已经破解了,因为日期似乎是正确的。直到我的 MySQL 查询不断返回看似随机的结果,我才意识到这个月实际上被视为一天。 $week_number
date('m/d/Y', strtotime('-1.5 years')) 返回今天(2012 年 6 月 18 日),06/18/2017。 这是为什么,有没有办法让 strtotime 处理小数
有没有办法转换strtotime与 NSDate 类似的字符串(例如:给定日期的“-3 天”)? 最佳答案 参见 NSDate 的 +dateWithNaturalLanguageString: 。我
我遇到了一个奇怪的问题。当我执行 strtotime 时,它不会考虑原始日期的小时部分,并且它总是在午夜返回。我尝试进行研究,但找不到任何具体内容。 我有什么遗漏吗? $original_date
我有一些 php 可以更改从数据库检索的时间格式: $selectedDate = $_GET['date']; $formatedDate = date("Y-d-m",strtotime($sel
我愿意 strtotime("2008-09-04") 其中 09 是月份,04 是日期,我得到的结果是: 1220500800 2008 年 9 月 4 日星期四 04:00:00 GMT。这 4
我正在使用 strtotime($my_time); 创建我在 php 数学中使用的时间戳。 变量$my_time 是从数据库中提取的HH:mm:ss 24h 格式。 我正在设置时间,但我不知道生产日
任何人都可以向我解释这个例子中的 strtotime 函数有什么问题?我正在尝试计算日期之间的差异。 PHP 5.6.0 (cli)(内置:2014 年 8 月 28 日 08:03:51)当我执行这
我正在根据用户输入的日期、月份和年份值构建时间戳。 假设用户输入了一些错误的值并且日期是不存在的“31-02-2012”,那么我必须得到一个错误的返回。但这里将其转换为附近的另一个日期。精确到:“02
这个问题在这里已经有了答案: php strtotime() messes with date of a different year (1 个回答) 关闭 9 年前。 我正在尝试使用 pickad
我有一些 php 可以更改从数据库检索的时间格式: $selectedDate = $_GET['date']; $formatedDate = date("Y-d-m",strtotime($sel
我愿意 strtotime("2008-09-04") 其中 09 是月份,04 是日期,我得到的结果是: 1220500800 2008 年 9 月 4 日星期四 04:00:00 GMT。这 4
我正在尝试使用 strtotime 转换以下日期: 07/09/2009 17:01:27 这是 9 月 7 日的欧洲/伦敦时区格式 该功能正在反转月份和日期,有什么解决方法吗? 谢谢 最佳答案 更改
我有一个循环,其中日期以不同的格式出现,例如对于某些值,它会像 '10-13-2013 04:31' ,对于某些值,它会像 >'2013-10-14T22:14:40-0700'。我尝试将其存储在数据
嗨,我有一个查询,它从数据库中选择 `timestamp` >= '".$monS."' AND `timestamp` < '".$sunE."' 以下是 $monS 和 $sunE 的定义方式:
我是一名优秀的程序员,十分优秀!