Zatla00
Zatla00

Reputation: 509

Date counter based on current date

I need to generate an html code of a list of 10 open days following the current day, by open days I mean business days (m,t,w,t and f), I'm using the following function to translate the date to french:

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();

And i want to generate the following result:

<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>

Please tell me if you need further information.

Thank you.

Upvotes: 0

Views: 224

Answers (2)

mixable
mixable

Reputation: 1158

Just create a loop, increase the days up to ten and ignore all non open days (Sat, Sun). date('N') is your friend to detect the weekday of a given date.

<?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++;
}

You also have to modify your date_f() function to use $temps as parameter.

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

Upvotes: 0

Matt Dodge
Matt Dodge

Reputation: 11142

Since you are only looking for MTWTF and you want the next 10 days, you can always safely look for the next 14 days and ignore the weekends and that will give 10 days. It won't work for holidays or anything like that but you can change it if you need to do that. I'll give you the pseudo-code here, I'll leave all the array mapping and text output to you

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>"
}

This relies on the 10 and 14 day assumption, if you wanted to change that number you could add some sort of counter and only increment the counter if you were looking at a weekday/non-holiday

Upvotes: 2

Related Questions