日期计数器基于当前日期

时间:2012-05-29 19:48:12

标签: php html

我需要生成一个包含当天后10个开放日列表的html代码,开放日我指的是工作日(m,t,w,t和f),我正在使用以下函数进行翻译法语的日期:

function f_date() {
    $temps = time();
    $jours = array('Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi');
    $jours_numero = date('w', $temps);
    $jours_complet = $jours[$jours_numero];
    $NumeroDuJour = date('d', $temps);
    $mois = array(' ', 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre');
    $mois_numero = date("n", $temps);
    $mois_complet = $mois[$mois_numero];
    $an = date('Y', $temps);
    $fr_temps = "$jours_complet, $NumeroDuJour $mois_complet $an";
    return "$fr_temps";
}
echo "<br/>".f_date();

我想生成以下结果:

<select name="ladate">
    <option selected="selected" value="Mardi, 29 mai 2012">29 mai 2012</option>
    <option value="Mercredi, 30 mai 2012">30 mai 2012</option></select>
    ....
    <option value="Vendredi, 15 juin 2012">15 juin 2012</option></select>
</select>

如果您需要更多信息,请告诉我。

谢谢。

2 个答案:

答案 0 :(得分:2)

由于您只是在寻找MTWTF并且您希望接下来的10天,因此您可以随时安全地查找接下来的14天并忽略周末,这将给出10天的时间。它不适用于假期或类似的东西,但如果你需要这样做,你可以改变它。我会在这里给你伪代码,我会把所有数组映射和文本输出留给你

for ($days_to_add : 1 to 14) {
    $new_date = date_add($days_to_add);

    // check the day of the week
    if (date('N', $new_date) >= 6) {
        // ignore it, it's a weekend
        continue;
    }

    // output the option tag for $new_date
    echo "<option ... </option>"
}

这取决于10天和14天的假设,如果你想改变这个数字,你可以添加某种计数器,只有在你看工作日/非假期时才增加计数器

答案 1 :(得分:0)

只需创建一个循环,将天数增加到十,并忽略所有非开放日(周六,周日)。 date('N')是您的朋友,可以检测指定日期的工作日。

<?php
$i = $openDay = 0;
while($openDay < 10) {
  $i++;
  $time = strtotime('+'.$i.' days');
  $day = date('N', $time);

  if ($day == 6 or $day == 7) { // ignore Saturdays and Sundays
    continue;
  }

  echo f_date($time).'<br>';
  $openDay++;
}

您还必须修改date_f()函数以使用$temps作为参数。

<?php
function f_date($temps = null) {
  // $temps = time();
  // ...
}