我如何修复这个PHP代码导致的月末错误?

时间:2022-06-01 18:44:04

The code below is what I'm using for a website menu which moves the link for the current month's page to the top of the link list on the change of the month.

下面的代码是我用于网站菜单的内容,该菜单将当月页面的链接移动到月份更改的链接列表顶部。

But this fails on the 31st of some months, such as April; I get two links to the same month for most of the links. I've read through the issues with the way php generates dates, but can't figure out how to change this code.

但这种情况在几个月的第31个失败了,比如四月;对于大多数链接,我获得了同一个月的两个链接。我已经通过php生成日期的方式阅读了这些问题,但无法弄清楚如何更改此代码。

Anyone php Ph.D's want to take a stab at it? Thanks

知识产权博士的任何人想要刺它吗?谢谢

<?php $month1 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+1 , date('d'), date('Y'))));
$month2 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+2 , date('d'), date('Y'))));
$month3 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+3 , date('d'), date('Y'))));
$month4 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+4 , date('d'), date('Y'))));
$month5 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+5 , date('d'), date('Y'))));
$month6 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+6 , date('d'), date('Y'))));
$month7 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+7 , date('d'), date('Y'))));
$month8 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+8 , date('d'), date('Y'))));
$month9 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+9 , date('d'), date('Y'))));
$month10 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+10 , date('d'), date('Y'))));
$month11 = (date('F', mktime(date('H'), date('i'), date('s'), date('m')+11 , date('d'), date('Y')))); ?>

<a href="http://mydomain.com/<?php echo strtolower(date('F')); ?>/" title="<?php echo ucfirst(date('F')); ?>"><?php echo (date('F')); ?></a><br />

<a href="http://mydomain.com/<?php echo strtolower($month1); ?>/" title="<?php echo $month1; ?>"><?php echo $month1; ?></a><br />

...(2 through 10)...

<a href="http://mydomain.com/<?php echo strtolower($month11); ?>/" title="<?php echo $month11; ?>"><?php echo $month11; ?></a><br />

5 个解决方案

#1


<?php

$current_month = date('n');
$MONTHS = array();
for ($m=0; $m<12; $m++) {
  $display_month = $m + $current_month;
  $MONTHS[] = date('F',mktime(1,1,1,$display_month,1,date("Y")));
}
foreach ($MONTHS as $month) {
  echo "
    <a
      href=\"http://mydomain.com/".strtolower($month)."\"
      title=\"$month\">$month</a><br />";
}
?>

#2


You can look into using strtotime() instead of mktime. Since in strtotime() to can do

你可以考虑使用strtotime()而不是mktime。因为在strtotime()中可以做到

strtotime("-1 day", time());

On the 1st of some month and it will go back one day exactly. Even counting in leap years.

在某个月的第一天,它将完全回到一天。甚至算在闰年。

#3


Use 1 instead of date('d') in your code; however, any time you see duplicated code, where only a number changes, you should be thinking about loops:

在代码中使用1代替日期('d');但是,只要你看到重复的代码,只有一个数字发生变化,你应该考虑循环:

<?php
for ($i = 0; $i < 12; $i++) {
    $month = date('F', mktime(0, 0, 0, date('m') + $i, 1, date('Y')));
?>

    <a href="http://mydomain.com/<?php echo strtolower($month); ?>" title="<?php echo $month; ?>"><?php echo $month; ?></a><br />

<?php
}
?>

#4


I have no idea if this solves your problem, as I'm not really sure I understood what the problem was, but I have an idea on how you could make this code a bit more readable:

我不知道这是否能解决你的问题,因为我不确定我是否理解问题是什么,但我知道如何让这段代码更具可读性:

<?php 
    $month1 = date('F', strtotime("+1 month"));
    $month2 = date('F', strtotime("+2 month"));
    $month3 = date('F', strtotime("+3 month"));
    $month4 = date('F', strtotime("+4 month"));
    $month5 = date('F', strtotime("+5 month"));
    $month6 = date('F', strtotime("+6 month"));
    $month7 = date('F', strtotime("+7 month"));
    $month8 = date('F', strtotime("+8 month"));
    $month9 = date('F', strtotime("+9 month"));
    $month10 = date('F', strtotime("+10 month"));
    $month11 = date('F', strtotime("+11 month"));
?>

(but i also agree with Chad Birch's note about using loops instead of repeating the code)

(但我同意Chad Birch关于使用循环而不是重复代码的说明)

<?php
    foreach(range(0,11) as $key){
        $months[$key]=date('F', strtotime("+{$key} month"));
    }
/* ... */
    foreach($months as $month){
        print "<a href='http://mydomain.com/".strtolower($month).
            "' title='".$month."'>".$month."</a><br />";
    }
?>

#5


Oh dear, that's some ugly code. You should really look into Loops (for/while/etc) and Arrays.

哦,亲爱的,这是一些丑陋的代码。你应该真正研究循环(for / while / etc)和Arrays。

The code you have there could be reduced to this:

你在那里的代码可以简化为:

Edited: my code had the same problem as the original, fixed using scronide's solution, thanks for pointing it out.

编辑:我的代码与原版有相同的问题,使用scronide的解决方案修复,感谢指出。

<?php
for ($i = 0; $i < 12; $i++)
{
    $months[$i] = date('F', mktime(0, 0, 0, date('m') + $i, 1, date('Y')));
}

for ($i = 0; $i < 12; $i++)
{
    print '<a href="http://mydomain.com/'.strtolower($months[$i]).'/" title="'.$months[$i].'">'.$months[$i]."</a><br />\n";
}
?>

Or if you don't mind combining the two functions (getting the month names, and printing the links) together, and you don't need the month names stored for anything else:

或者,如果您不介意将两个函数(获取月份名称和打印链接)组合在一起,并且您不需要为其他任何内容存储月份名称:

<?php
for ($i = 0; $i < 12; $i++)
{
    $month = date('F', mktime(0, 0, 0, date('m') + $i, 1, date('Y')));
    print '<a href="http://mydomain.com/'.strtolower($month)."/\" title=\"$month\">$month</a><br />\n";
}
?>

#1


<?php

$current_month = date('n');
$MONTHS = array();
for ($m=0; $m<12; $m++) {
  $display_month = $m + $current_month;
  $MONTHS[] = date('F',mktime(1,1,1,$display_month,1,date("Y")));
}
foreach ($MONTHS as $month) {
  echo "
    <a
      href=\"http://mydomain.com/".strtolower($month)."\"
      title=\"$month\">$month</a><br />";
}
?>

#2


You can look into using strtotime() instead of mktime. Since in strtotime() to can do

你可以考虑使用strtotime()而不是mktime。因为在strtotime()中可以做到

strtotime("-1 day", time());

On the 1st of some month and it will go back one day exactly. Even counting in leap years.

在某个月的第一天,它将完全回到一天。甚至算在闰年。

#3


Use 1 instead of date('d') in your code; however, any time you see duplicated code, where only a number changes, you should be thinking about loops:

在代码中使用1代替日期('d');但是,只要你看到重复的代码,只有一个数字发生变化,你应该考虑循环:

<?php
for ($i = 0; $i < 12; $i++) {
    $month = date('F', mktime(0, 0, 0, date('m') + $i, 1, date('Y')));
?>

    <a href="http://mydomain.com/<?php echo strtolower($month); ?>" title="<?php echo $month; ?>"><?php echo $month; ?></a><br />

<?php
}
?>

#4


I have no idea if this solves your problem, as I'm not really sure I understood what the problem was, but I have an idea on how you could make this code a bit more readable:

我不知道这是否能解决你的问题,因为我不确定我是否理解问题是什么,但我知道如何让这段代码更具可读性:

<?php 
    $month1 = date('F', strtotime("+1 month"));
    $month2 = date('F', strtotime("+2 month"));
    $month3 = date('F', strtotime("+3 month"));
    $month4 = date('F', strtotime("+4 month"));
    $month5 = date('F', strtotime("+5 month"));
    $month6 = date('F', strtotime("+6 month"));
    $month7 = date('F', strtotime("+7 month"));
    $month8 = date('F', strtotime("+8 month"));
    $month9 = date('F', strtotime("+9 month"));
    $month10 = date('F', strtotime("+10 month"));
    $month11 = date('F', strtotime("+11 month"));
?>

(but i also agree with Chad Birch's note about using loops instead of repeating the code)

(但我同意Chad Birch关于使用循环而不是重复代码的说明)

<?php
    foreach(range(0,11) as $key){
        $months[$key]=date('F', strtotime("+{$key} month"));
    }
/* ... */
    foreach($months as $month){
        print "<a href='http://mydomain.com/".strtolower($month).
            "' title='".$month."'>".$month."</a><br />";
    }
?>

#5


Oh dear, that's some ugly code. You should really look into Loops (for/while/etc) and Arrays.

哦,亲爱的,这是一些丑陋的代码。你应该真正研究循环(for / while / etc)和Arrays。

The code you have there could be reduced to this:

你在那里的代码可以简化为:

Edited: my code had the same problem as the original, fixed using scronide's solution, thanks for pointing it out.

编辑:我的代码与原版有相同的问题,使用scronide的解决方案修复,感谢指出。

<?php
for ($i = 0; $i < 12; $i++)
{
    $months[$i] = date('F', mktime(0, 0, 0, date('m') + $i, 1, date('Y')));
}

for ($i = 0; $i < 12; $i++)
{
    print '<a href="http://mydomain.com/'.strtolower($months[$i]).'/" title="'.$months[$i].'">'.$months[$i]."</a><br />\n";
}
?>

Or if you don't mind combining the two functions (getting the month names, and printing the links) together, and you don't need the month names stored for anything else:

或者,如果您不介意将两个函数(获取月份名称和打印链接)组合在一起,并且您不需要为其他任何内容存储月份名称:

<?php
for ($i = 0; $i < 12; $i++)
{
    $month = date('F', mktime(0, 0, 0, date('m') + $i, 1, date('Y')));
    print '<a href="http://mydomain.com/'.strtolower($month)."/\" title=\"$month\">$month</a><br />\n";
}
?>