Hello! I wanted to make a php script that will count how much is left before the New Year (in days, hours, minutes and seconds). I would be grateful if you throw the code.
- oneI have a feeling that the hashcode instead of Google search is used. here they will write the code for you, and the fact that Google offers thousands of scripts in the search, already written, and even with a description ... Eh where the youngsters are heading, what will happen in 5 years. someone asks a question, and immediately give him the code and documentation, and instead work, and he will get the babos :) - Artem
- @Shrek well, there are a lot of examples in the network, but only people will really prompt correctly, not Google - Mikhail Nikolaev
- and we have been writing machines for 10 years, did not know, did not know. - Artem
- oneStrange, is it not better to use JS? - culebre
- one@culebre, quiet! otherwise he will think now, think, and ask to write to JS! For [countdown timer] [1] will not find! [1]: hashcode.ru/questions/36366/timer- back - count - knes
|
4 answers
I will post it too, because when I started writing, there were no answers ... the work did not disappear :)
$currentYear = date('Y'); // получаем текущий год $newYear = mktime(0, 0, 0, 1, 1, $currentYear + 1); // высчитываем дату Нового Года $seconds = $newYear - time(); $days = 0; $hours = 0; $minutes = 0; $oneMinute = 60; $oneHour = 60 * $oneMinute; $oneDay = $oneHour * 24; if ($seconds / $oneDay > 0) { $days = (int)($seconds / $oneDay); $seconds -= $days * $oneDay; } if ($seconds / $oneHour > 0) { $hours = (int)($seconds / $oneHour); $seconds -= $hours * $oneHour; } if ($seconds / $oneMinute > 0) { $minutes = (int)($seconds / $oneMinute); $seconds -= $minutes * $oneMinute; } printf('К Новому Году осталось: %02d дней %02d часов %02d минут и %02d секунд', $days, $hours, $minutes, $seconds); - And how do you solve the time zone problem you described? =)) - knes
- and in timestamp there is no timezone. There is always GMT, there is one timestamp, subtracting the second and we get the net number of seconds in the interval of interest. - Alex Kapustin
- Then I do not see a fundamental difference in the code. I'm stupid. Well, in a sense, in this question. - knes
- And here there is not much difference with your code :). I posted it because I wrote it, at the time of writing the answers were not. And about the timezone, this refers to the answer @Palmervan PS: It's already evening, maybe I just don't understand something :) - Alex Kapustin
|
You again...
<?php $ny = mktime(0, 0, 0, 01, 01, 2012); $timeUntilNY = $ny - time(); $days = floor($timeUntilNY/3600/24); $hours = floor($timeUntilNY%(3600*24)/3600); $mins = floor(($timeUntilNY%3600)/60); $secs = $timeUntilNY%60; printf('До нового года: %d дней %d часов %d минут %d секунд ',$days,$hours,$mins,$secs); |
$realtime = mktime(0, 0, 0, 01, 01, 2012); $futuretime = $realtime - time(); echo date("До нового года: j дней h часов i минут s секунд", $futuretime); - oneThis is not a valid code. Because you will have a difference in the number of hours your timezone. You can use gmdate on a pinch but it will work only for the period <one month before the New Year. - Alex Kapustin
- Yes, I have already seen, there is an error ... a couple of minutes are left until the end of the working day, there is no time to think over) - Palmervan
- 2Yeah ... And it will work only for December. If this is run in November, then everything is giknet. - knes
- @knes, well, I meant it "but it will work only for the period <one month before the New Year." It was just that a verbal stupor was in an attempt to describe it :) - Alex Kapustin
- So I didn’t eat this phrase =))) - knes
|
My version:
$date = date_create(); //текущая дата $datka = date_format($date, 'dnY'); $newYear = 12; //последний месяц года $arr = explode('.', $datka); $month = []; //здесь будет количество дней, в каждом месяце текущего года for($i = 1; $i <=12; $i++) { $month[$i] = date("t", mktime(0, 0, 0, $i, $arr[0], $arr[2])); } echo '<pre>'; print_r($month); echo '</pre>'; $ostatok = 0; for($j = $arr[1]; $j <=12; $j++) { //$arr[1]отсчёт начинается с текущего месяца (складываются все дни) $ostatok= $ostatok + $month[$j]; } echo 'Количество дней до Нового года: ' . ($ostatok - $arr[0]); |