教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP strtotime计算上一个月的今天问题解决

PHP strtotime计算上一个月的今天问题解决

发布时间:2018-10-26   编辑:jiaochengji.com
教程集为您提供PHP strtotime计算上一个月的今天问题解决等资源,欢迎您收藏本站,我们将为您提供最新的PHP strtotime计算上一个月的今天问题解决资源
今天小编来给大家介绍一个关于PHP strtotime计算上一个月的今天问题解决,如果你碰到计算上一个月今天有问题不防进入参考。

PHP,上一个月

strtotime 有个小问题

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4719')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4719>

> php -r”echo date(‘Ymd000000′,strtotime ( ‘-1 month’, strtotime ( ’201307310000′ ) ));”
20130701000000#
> php -r”echo date(‘Ymd000000′,strtotime ( ‘-1 month’, strtotime ( ’201308010000′ ) ));”
20130701000000#

搜了一下,下面的方法更准确一些

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7705')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7705>

$time = strtotime("2011-03-31");


函数 mktime,它可以取得日期的时间戳:

int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] )

根据给出的参数返回 Unix 时间戳。时间戳是一个长整数,包含了从 Unix 纪元(January 1 1970 00:00:00 GMT)到给定时间的秒数。

参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。
所以可以通过它来计算

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6231')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6231>

function last_month_day($time){
    $strtime=mktime(date('h',$time),date('i',$time),date('s',$time),date('m',$time)-1,date('d',$time),date('Y',$time));
    echo date('Y-m-d',$strtime);
}
 last_month_day(strtotime("2012-03-31"));


输出的结果是2012-03-02;按理说应该是输出2012-02-31????笨蛋2月有31号吗?没,有30号吗,没?有29号吗?..这个..可以有…
PHP给我们处理了这种情况,他会多出几天按下个月来算.
2012年2月最后一天是2012-02-29那么31号比29多两天,所以PHP就累加到下个月来处理 就是2012-03-02啦.
差点忘了..那么星期几怎么算呢??不用算啦..PHP给我们准备好了.
date(‘w’,$strtime);输出的就是一周中的第几天.就是星期几了.

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9925')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9925>

/**
 * 计算上一个月的今天,如果上个月没有今天,则返回上一个月的最后一天
 * @param type $time
 * @return type
 */
function last_month_today($time){
    $last_month_time = mktime(date("G", $time), date("i", $time),
                date("s", $time), date("n", $time), 0, date("Y", $time));
    $last_month_t =  date("t", $last_month_time);

    if ($last_month_t < date("j", $time)) {
        return date("Y-m-t H:i:s", $last_month_time);
    }

    return date(date("Y-m", $last_month_time) . "-d", $time);
}

echo last_month_today($time);

您可能感兴趣的文章:
PHP计算上一个月的今天的代码
PHP strtotime计算上一个月的今天问题解决
总结了php时间处理问题
php中strtotime时间函数使用详解
PHP常用的日期和时间总结
PHP计算上一个月的今天 今天是星期几
PHP日期时间运算方法总结
php获取本周、本月第一天与最后一天的时间戳
php日期与时间运算实例分享
php计算上个月的今天的函数代码

[关闭]
~ ~