php date('d')连续两天计算相同的输出 [英] php date('d') calculates same output for two consecutive days

查看:143
本文介绍了php date('d')连续两天计算相同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过PHP构建一个日历,并且这样做的方式在几天内被写入两次。



我复制了这个小脚本中的行为:

 <?php 
//
//定义从$ b $开始的日期b //
$ d = 26;
$ m = 10;
$ y = 2013;
date_default_timezone_set('CET');
$ time = mktime(0,0,0,$ m,$ d,$ y);

//
//计算10年
//
($ i = 0; $ i <3650; $ i ++){
$ tomorrowTime = $ time +(60 * 60 * 24);

//
//如果第二天有相同的日期('d')结果$ echo
//
if(date('d' $ time)== date('d',$ tomorrowTime)){
echo date('dm-Y',$ time)计算了两次... \\\
;
}

$ time = $ tomorrowTime;
}

?>

这是我得到的:

  27-10-2013计算了两次... 
26-10-2014计算了两次...
25-10-2015计算了两次...
30-10-2016计算了两次...
29-10-2017计算了两次...
28-10-2018计算了两次...
27计算了两个... ...
25-10-2020计算了两次...
31-10-2021计算了两次...
30-10-2022被计算两次...

当我定义 $ time as 0(unix epoch),我没有得到相同的行为。
使用 mktime()有什么问题吗?
还是11月只是尴尬?



干杯,
Jeroen

解决方案

此语句应该更好地防范闰秒等等:

  $ tomorrowTime = strtotime('+ 1天,$时间); 


I'm building a calendar through PHP and the way I'm doing this results on some days being written twice.

I replicated the behaviour in this little script:

<?php
//
// define a date to start from
//
$d = 26;
$m = 10;
$y = 2013;
date_default_timezone_set('CET');
$time = mktime(0, 0, 0, $m, $d, $y);

//
// calculate 10 years
//
for($i=0;$i<3650;$i++){ 
  $tomorrowTime = $time + (60 * 60 * 24);

  //
  // echo date if the next day has the same date('d') result
  //
  if(date('d',$time)==date('d',$tomorrowTime)){
    echo date('d-m-Y',$time)." was calculated twice... \n";
  }

  $time = $tomorrowTime;
}

?>

This is what I get:

27-10-2013 was calculated twice... 
26-10-2014 was calculated twice... 
25-10-2015 was calculated twice... 
30-10-2016 was calculated twice... 
29-10-2017 was calculated twice... 
28-10-2018 was calculated twice... 
27-10-2019 was calculated twice... 
25-10-2020 was calculated twice... 
31-10-2021 was calculated twice... 
30-10-2022 was calculated twice... 

When I define $time as 0 (unix epoch), I don't get the same behaviour. Is there something wrong with using mktime()? Or is November just being awkward?

Cheers, Jeroen

解决方案

This statement should guard better against leap seconds and such:

$tomorrowTime = strtotime('+1 days', $time);

这篇关于php date('d')连续两天计算相同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆