/** * test next_lottery_date function * * @return void */ public function testnext_lottery_date() { $w0 = mktime(0, 0, 0, 7, 31, 2016); $w7 = mktime(0, 0, 0, 8, 7, 2016); $fl = mktime(20, 0, 0, 8, 3, 2016); $sl = mktime(20, 0, 0, 8, 6, 2016); $nfl = mktime(20, 0, 0, 8, 10, 2016); $cases = []; $expected = []; $cases[] = $w0; $expected[] = $fl; // case 1 $cases[] = $fl - 1; $expected[] = $fl; // case 2 $cases[] = $fl; $expected[] = $sl; // case 3 $cases[] = $sl - 1; $expected[] = $sl; // case 4 $cases[] = $sl; $expected[] = $nfl; // case 5 $cases[] = $w7 - 1; $expected[] = $nfl; // case 6 $cases[] = $w7; $expected[] = $nfl; // case 7 echo "\n"; for ($i = 0; $i < 7; $i++) { echo "case: " . date("Y-m-d H:i:s", $cases[$i]) . ", result: " . date("Y-m-d H:i:s", getNextLotteryDate($cases[$i])) . " , expected: " . date("Y-m-d H:i:s", $expected[$i]) . "\n"; $this->assertEquals($expected[$i], getNextLotteryDate($cases[$i])); } return null; }
<?php date_default_timezone_set('Europe/Dublin'); $unix_time = time(); echo "unix time:" . $unix_time . "\n"; echo "w day(0 for sunday,6 for sat.):" . date('w', $unix_time) . "\n"; echo "current date:" . date('Y-m-d H:i:s w', $unix_time) . "\n"; echo getNextLotteryDate($unix_time); // @param int $cur , current unix time // @return int , next lottery date function getNextLotteryDate($cur) { /* $dtz = new DateTimeZone('Europe/Dublin'); $time_in_dublin = new DateTime('now', $dtz); $tz_offset = $dtz->getOffset( $time_in_dublin ); $week_sec = 7*86400; $diff_to_sat20 = 2*86400 + 20*3600; $bsat20 = ((int)(($cur - $diff_to_sat20 +$tz_offset)/($week_sec)))*($week_sec) + $diff_to_sat20 - $tz_offset + 4*86400; if ($bsat20 <=$cur) { return $bsat20 + 3*86400; } return $bsat20; */ $today = mktime(0, 0, 0, date("m", $cur), date("d", $cur), date("Y", $cur)); $wday = date('w', $cur); $wstart = $today - $wday * 86400; // this week lottery dates