string(6) "2013-12" [2] => string(6) "2014-1" [3] => string(6) "2014-2" [4] => string(6) "2014-3" [5]"/> string(6) "2013-12" [2] => string(6) "2014-1" [3] => string(6) "2014-2" [4] => string(6) "2014-3" [5]">
搜索
首页后端开发php教程PHP 怎么列出来某一个日期距离现在的日期

比如
$aa="2013-12-25";
$bb=time();
我想列出2013-12距离现在的月份

array(12) {
[1] => string(6) "2013-12"
[2] => string(6) "2014-1"
[3] => string(6) "2014-2"
[4] => string(6) "2014-3"
[5] => string(6) "2014-...."
}

现在只能列出当年的当前月份
for ($i = 1; $i $dates[$i] = date('Y-'.$i, strtotime(date("Y-m-d")));
}

回复内容:

比如
$aa="2013-12-25";
$bb=time();
我想列出2013-12距离现在的月份

array(12) {
[1] => string(6) "2013-12"
[2] => string(6) "2014-1"
[3] => string(6) "2014-2"
[4] => string(6) "2014-3"
[5] => string(6) "2014-...."
}

现在只能列出当年的当前月份
for ($i = 1; $i $dates[$i] = date('Y-'.$i, strtotime(date("Y-m-d")));
}

先说感想吧

  • 这种“要知道循环多少次略有困难”的问题用while会让逻辑清晰不少
  • strtotime大杀器
  • PHP4.3+
  • //我的代码又短又清楚,哼
<code>php</code><code>//http://3v4l.org/vuef4
function monthToToday($past, $now, $format = 'Y-m') {
    $current = strtotime(date('Y-m-1', strtotime($past)));
    $result = array();
    while($current </code>

两者时间戳相减

<code><?php // http://3v4l.org/HkTl8
function diffMonth($month, $format = "Y-m", $monthDiffed = "now", $dateTimeZone = false) {
    if(!$dateTimeZone) $dateTimeZone = new DateTimeZone("Asia/Shanghai");

    $monthDiffed = new DateTime($monthDiffed, $dateTimeZone);
    $month = new DateTime($month, $dateTimeZone);
    if($month > $monthDiffed) {
        $m = $month;
        $month = $monthDiffed;
        $monthDiffed = $m;
    }
    $monthDiffed = $monthDiffed->modify("last day of -1 month");
    $month = $month->modify("first day of +1 month");

    $result = array();
    while($month format( $format );
        $month = $month->modify("+1 month");
    }
    return $result;
}

print_r( diffMonth("2014-6") );
print_r( diffMonth("2014-6", "Y/m", "2013-6") );
</code>

<code>$aa="2003-02-25";

$bb = date("Y-m-d",time());


function datediffage($before, $after) {

    $b = getdate($before);
    $a = getdate($after);


$y=$m=0;

if ($a['mon']==1) { //1月,借年
    $y=$a['year']-$b['year']-1;$m=$a['mon']-$b['mon']+12;
}
else {
    if ($a['mon']>=$b['mon']+1) { //借月后,月相减为正
        $y=$a['year']-$b['year'];$m=$a['mon']-$b['mon']-1;
    }
    else { //借月后,月相减为负,借年
        $y=$a['year']-$b['year']-1;$m=$a['mon']-$b['mon']+12-1;
    }
}

$datearr = array();

$totalm = "";
if( $y > 0 ){
    $totalm = $y*12;//大于1年,转换成月份数
}

$totalm += $m;

for ($i=0; $i </code>

}

print_r(datediffage(strtotime($aa),strtotime($bb)));

圆代码来自:http://my.oschina.net/u/223350/blog/293687
感谢原作者,有修改。

<code>echo (new DateTime('2013-12'))->diff(new DateTime(date('Y-m')))->format('%y year %m month %d days');
</code>

结果是从目标时间到今天已经过去几年几月几天

年x12+month = 已经过去几个月

<code>$date = new DateTime('2013-12');
$now = date('Ym');
$months = array();
while ($date->format('Ym')format('Y-m'));
    $date->modify('+1 month');
}
print_r($months);
</code>

获取目标时间到现在的月份详情

$aa = '2013-12-25';
$a = strtotime($aa);
$b = array();
while($a

写完发现和mcfog思路差不多

这个问题貌似都有问题啊,
时间差出来了,那么,你一个月是按28天算,还是30天或31天算呢?
年是按365天还是366天算呢?

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能