`

strtotime("+1 month", $strtimes) 导致时间戳错误

阅读更多

如果要获取当前时间中未来一个自然月的这个linux时间戳时

 

使用  $nexttime = strtotime("+1 month", $strtimes); 是不精确的,尤其$strtimes无效时。

 

因为PHP “+1 month” 默认是30天,strtotime “+1 month”大月31、小月30、2月是28因此会导致时间戳错误

 

要使用

$nexttime =mktime(date("H"),date("i"),date("s"),date("m")+1,date("d"),date("Y"));

 

如果说时间是2013-02-29(此时间的linux值实现为2013-03-01),而需要逻辑处理后是2013-03-29;所以需要做加一个月处理,万一在处理时间的时候是 strtotime("+1 month",strtotime("2013-02-29"))的方法;得出的时间是:2013-04-01;因此会导致一系列的时间相减为负数。

 

注:别说 2013-02-29 是不存在,这全世界人民都知道,可有时候程序中会存在的。

 

实例:

$str1 = strtotime("2013-02-29 14:35:59");

echo (time() - strtotime("+1 month",$str1))/(24*3600);  //为负

$nexttime =mktime(date("H",14),date("i",35),date("s",59),date("m",02)+1,date("d",29),date("Y",2013));

echo (time() - $nexttime)/(24*3600);  //为正

分享到:
评论

相关推荐

    Javascript模仿php中strtotime()函数实现时间字符串转时间戳方法

    强制返回整数型时间戳,如果错误返回false document.write(strtotime('now')); document.write(strtotime('next Sunday')); document.write(strtotime('last month')); document.write(strtotime('+1 weeks')); ...

    PHP时间戳 strtotime()使用方法和技巧

    在php中我想要获取时间戳有多种方法,最常用的就是使用time函数与strtotime()函数把日期转换成时间戳了,下面我来给大家分享一下时间戳函数 strtotime用法。获取指定的年月日转化为时间戳:pHP时间戳函数获取指定...

    php strtotime 函数UNIX时间戳

    int strtotime ( string time [, int now])本函数预期接受一个包含英文日期格式的字符串并尝试将其解析为 UNIX 时间戳。

    PHP时间戳与日期之间转换

    PHP时间戳与日期之间转换 echo(strtotime("now")); echo(strtotime("3 ... echo(strtotime("+1 week 3 days 7 hours 5 seconds")); echo(strtotime("next Monday")); echo(strtotime("last Sunday")); ?>

    php使用date和strtotime函数输出指定日期的方法

    本文实例讲述了php使用date和strtotime函数输出指定日期的方法。分享给大家供大家参考。...strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳 复制代码 代码如下:date_default_timezone_set(‘Asia

    PHP获取本周所有日期或者最近七天所有日期的方法

    希望可以帮助到有需要的朋友 获取本周所有日期: /** * 获取本周所有日期 */ function get_week($time = '', $format='Y-m-d'){ $time = $time !... $date[$i] = date($format ,strtotime( '+' . $

    解析php时间戳与日期的转换

    大家也许对PHP时间戳已经有所了解,那么我们如何应用它来获取具体的日期呢?我们今天来为大家介绍一下PHP时间戳获取当前...一,获取指定日期的unix时间戳 strtotime(”2009-1-22〃) 示例如下:echo strtotime(”2009-

    PHP中获取时间的下一周下个月的方法

    代码如下: //通常用于定制服务的时候使用,比如包月会员,包年等等 //获取当前时间过一个月的时间,以DATETIME格式显示 date(‘Y-m-d H:i:s’,strtotime(‘+1 month’)) //获取当前时间过一个月的时间,以时间戳格式...

    PHP 时间转换Unix时间戳代码

    $time2 = strtotime($time1); echo $time2; echo date(‘Y-m-d h:i:s’,$time2); ?> 您可能感兴趣的文章:php实现兼容2038年后Unix时间戳转换函数PHP中UNIX时间戳和日期间的转换与计算实例php 创建以UNIX时间戳...

    PHP 简单日历实现代码

    //获得本月1号星期几 $monthday=date(“t”); //本月多少天 $startlow=($oneweekday==0)?7:$oneweekday; //从第几列开始 for($a=1,$b=$startlow;$a<=$monthday;$a++,$b++){ $ary[$b]=$a; //用数组控制日期在哪里...

    PHP获取时间排除周六、周日的两个方法

    今天和大家分享一个获取10天后的一个时间戳的函数,程序的关键是,他可以不去算周六日哦。如果你有别的需求。可以改成N天的哦。反正就不算周六日。哈哈。 //方法一: <?php $now = time(); //指定日期用法 $now...

    php获取当前月与上个月月初及月末时间戳的方法

    本文实例讲述了php获取当前月与上个月月初及... '-1'; $endDay = $thisyear . '-' . $thismonth . '-' . date('t', strtotime($startDay)); $b_time = strtotime($startDay);//当前月的月初时间戳 $e_time = strtotime

    PHP使用strtotime获取上个月、下个月、本月的日期

    今天写程序的时候,突然发现了很早以前写的获取月份天数的函数,经典的switch版,但是获得上月天数的时候,我只是把月份-1了,估计... $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day")); return ar

    php中strtotime函数用法详解

    strtotime(字符串$时间[,诠释$现在])int strtotime(string $time [,int $now] 该函数期望得到一个包含美国英语日期格式,并会尝试解析成一个Unix时间戳(多少秒自1970年1月1日00:00:00星期一该格式),相对于现在提供的...

    PHP时间戳格式全部汇总 (获取时间、时间戳)

    希望对新手的学习有所帮助! 一,PHP时间戳函数获取指定日期的unix时间戳 strtotime(”2009-1-22″) 示例如下:...(1)打印明天此时的时间戳strtotime(”+1 day”) 当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2

    PHP中strtotime函数使用方法分享

    一,获取指定日期的unix时间戳 ...(1)打印明天此时的时间戳strtotime(“+1 day”) 当前时间: 1.echo date(“Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25 指定时间: 1.echo date(“Y-m-d H:i:s”,strtotim

    php 获取一个月第一天与最后一天的代码

    $lastday = date(‘Y-m-d’, strtotime(“$firstday +1 month -1 day”)); return array($firstday, $lastday); } 您可能感兴趣的文章:用php获取本周,上周,本月,上月,本季度日期的代码PHP显示今天、今月、上月、...

    PHP setTime 设置当前时间的代码

    //设置每天早上9点 return date(“Y-m-d H:i”,strtotime(“+$m minute”,$date->getTimestamp())); } 您可能感兴趣的文章:php UNIX时间戳用法详解php自定义时间转换函数示例php获取当前月与上个月月初及月末...

    PHP strtotime函数详解

    先看手册介绍: strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳 格式:int strtotime ( string $time [, int $now ] ) 本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳...

Global site tag (gtag.js) - Google Analytics