Advertisement
dimipan80

Time Until New Year

Dec 2nd, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.72 KB | None | 0 0
  1. /* Write a PHP script TimeUntilNewYear.php. Use the built-in function getdate() to get the current date and time.
  2. Print how many hours, minutes and seconds are left until New Year and how many days, hours, minutes and seconds
  3. are left in a counter format . The examples show the current date and time in "d-m-Y G:i:s" format.
  4. Use the current time. */
  5.  
  6. <?php
  7. date_default_timezone_set('Europe/Sofia');
  8. function getTimeIntervalUntilNewYear($inputDateStr = null) {
  9.     $currentTime = getdate();
  10.     $currentYear = $currentTime['year'];
  11.     $currentTime = $currentTime[0];
  12.  
  13.     if ($inputDateStr !== null) {
  14.         $inputDate = preg_split("/\D+/", $inputDateStr);
  15.         if (count($inputDate) == 6) {
  16.             $currentTime = mktime($inputDate[3], $inputDate[4], $inputDate[5],
  17.                 $inputDate[1], $inputDate[0], $inputDate[2]);
  18.             $currentYear = $inputDate[2];
  19.         }
  20.     }
  21.  
  22.     $newYearTime = mktime(23, 59, 59, 12, 31, $currentYear);
  23.     $totalSeconds = $newYearTime - $currentTime;
  24.     $totalMinutes = (int)($totalSeconds / 60);
  25.     $totalHours = (int)($totalSeconds / 3600);
  26.     $days = (int)($totalSeconds / 86400);
  27.     $seconds = $totalSeconds % 86400;
  28.     $hours = (int)($seconds / 3600);
  29.     $seconds = $seconds % 3600;
  30.     $minutes = (int)($seconds / 60);
  31.     $seconds = $seconds % 60;
  32.  
  33.     echo "<p>Hours until new year : $totalHours</p>\n<p>Minutes until new year : " .
  34.         number_format($totalMinutes, 0, ' ', ' ') . "</p>\n" .
  35.         "<p>Seconds until new year : " . number_format($totalSeconds, 0, ' ', ' ') . "</p>\n" .
  36.         "<p>Days:Hours:Minutes:Seconds $days:$hours:$minutes:$seconds</p>\n";
  37. }
  38.  
  39. getTimeIntervalUntilNewYear('12-08-2014 11:08:47');
  40. getTimeIntervalUntilNewYear('12-08-2014 13:07:09');
  41. getTimeIntervalUntilNewYear('12-02-2015 20:30:10');
  42. getTimeIntervalUntilNewYear();
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement