Skip to content

Instantly share code, notes, and snippets.

@cotcotquedec
Created June 23, 2016 13:37
Show Gist options
  • Save cotcotquedec/53f970ea532533e248a66bc50c3e7e01 to your computer and use it in GitHub Desktop.
Save cotcotquedec/53f970ea532533e248a66bc50c3e7e01 to your computer and use it in GitHub Desktop.
Test recrutement php lvl1
<?php
$the_date = date('Y-m-d', rand(0, time()));
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="EXPIRES" content="0" />
<meta http-equiv="Pragma" content="no-cache" />
<title>Petit test : Bissextile!</title>
</head>
<body>
<h2>Objectif : </h2>
<div class="rules">- Indiquer la prochaine année où elle tombe le même jour de la semaine!</div>
<h2>Réalisation : </h2>
<div id="todo"></div>
</body>
</html>
@davaxi
Copy link

davaxi commented Jun 24, 2016

Cadeau:

<?php

$theDate = date('Y-m-d', rand(0, time()));
echo 'Date de référence: ', $theDate, PHP_EOL;
$epoch = strtotime($theDate);
$dayInWeek = date('w', $epoch);

$year = (int)date('Y', $epoch);
$monthDay = date('m-d', $epoch);

/*
 * Or
 *
 * $datePart = explode('-', $theDate);
 * $year = (int)array_shift($datePart[0]);
 * $month = implode('-', $datePart);
 *
 */

do {
    $year++;
    $currentDate = $year . '-' . $monthDay;
    $currentEpoch = strtotime($currentDate);
    $currentDayInWeek = date('w', $currentEpoch);
    if ($currentDayInWeek === $dayInWeek) {
        echo 'Prochaine occurence en ', $year, PHP_EOL;
        break;
    }

} while(true);

@yoann-losbar
Copy link

$theDate = date('Y-m-d', rand(0, time()));
$dateObject = DateTime::createFromFormat('Y-m-d', $theDate);
$referenceDay = $dateObject->format("w");

while (1) {
    $dateObject->modify('+1 year');
    if ($dateObject->format("L") && $dateObject->format("w") == $referenceDay) {
        echo 'From ' . $theDate . ' the next occurence is ' . $dateObject->format("Y");
        exit();
    }
}

@janisVincent
Copy link

$today = new \DateTime();
$dayofWeek = $today->format('w');
$date = clone $today;
$interval = new \DateInterval('P1Y');

do {
    $date->add($interval);

    if ($date->format('w') == $dayofWeek) {
        echo "La réponse est " . $date->format('Y') . " (" . $date->format('D. d/m/Y') . ")";
        break;
    }
}
while (true);

@cotcotquedec
Copy link
Author

Je n'avais pas vu qu'il y a avait des réponses. Ben je vais continuer a faire des tests!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment