How to make a countdown (days) on the site, with every seven days auto-update count? There is a code:

function downcounter($date){ $check_time = strtotime($date) - time(); if($check_time <= 0){ return false; } $days = floor($check_time/86400); $hours = floor(($check_time%86400)/3600); $minutes = floor(($check_time%3600)/60); $seconds = $check_time%60; $str = $days; return $str; 

}

 //$nextWeek = date("Ymd",time()+7*24*60*60); //$timer = downcounter($nextWeek); $timer = downcounter('2013-02-19 23:59:59'); 

It looks like you need to make a database and store the date there and if the current system date == the date from the database, then execute: time () + (7 * 24 * 60 * 60) - and record the new date in the database. QUESTION: How to do without a DB, I do not want to do a DB for the sake of one function. ??

  • Or maybe just focus on the current day of the week and rely not on 7 days but let's say until next Friday? - Mike
  • Great idea. Pliz tell me how to change the code so that it works in days of the week. I would be very, very grateful. - amijin
  • one
    date ('w') will give the current day of the week, 0 - Sunday, 1 - Monday, etc. adjust it to our calendar, i.e. 0 change to 7 and find the difference in days to the desired day of the week - Mike
  • Updated the answer. added function to recount. - E_p

1 answer 1

http://php.net/manual/en/function.date.php (Format Section)

 <?php $datetime = new DateTime(); echo $datetime->format('w'); echo $datetime->format('N'); 

And there you can count on the day of the week.

 <?php function daysTo($dayOfWeek) { $delta = (integer) date('N') - $dayOfWeek; //Что бы промежуток был 0 - 6 return $delta > 0 ? 7 - $delta : -$delta; // Что бы промежуток был 1 - 7 // return $delta >= 0 ? 7 - $delta : -$delta; } echo daysTo(1) . PHP_EOL; // Понедельник echo daysTo(2) . PHP_EOL; // Вторник echo daysTo(3) . PHP_EOL; // Среда echo daysTo(4) . PHP_EOL; // Четверг echo daysTo(5) . PHP_EOL; // Пятница echo daysTo(6) . PHP_EOL; // Суббота echo daysTo(7) . PHP_EOL; // Воскресенье