Pen PHP nedteller


/ Published in: PHP
Save to your folder(s)

This is the Aidan Lister PHP countdown, translated for use in Norway.
No more 133 days untill christmas, but insted 3 months and 3 weeks.


Copy this code and paste it in your HTML
  1. <?
  2. $day = 21; // Day of the countdown
  3. $month = 12; // Month of the countdown
  4. $year = 2009; // Year of the countdown
  5. $hour = 23; // Hour of the day (east coast time)
  6.  
  7. $sekunder = ((mktime ($hour,0,0,$month,$day,$year)-time(void))); // sekunder
  8.  
  9. // s���¥ Aidans kode:
  10.  
  11. /**
  12.  * A function for making time periods readable
  13.  *
  14.  * @author Aidan Lister <[email protected]>
  15.  * @version 2.0.0
  16.  * @link http://aidanlister.com/2004/04/making-time-periods-readable/
  17.  * @param int number of seconds elapsed
  18.  * @param string which time periods to display
  19.  * @param bool whether to show zero time periods
  20.  */
  21. function time_duration($seconds, $use = null, $zeros = false)
  22. {
  23. // Define time periods
  24. $periods = array (
  25. '&aring;r' => 31556926,
  26. 'm&aring;neder' => 2629743,
  27. 'uker' => 604800,
  28. 'dager' => 86400,
  29. 'timer' => 3600,
  30. 'minutter' => 60,
  31. 'sekunder' => 1
  32. );
  33.  
  34. // Break into periods
  35. $seconds = (float) $seconds;
  36. foreach ($periods as $period => $value) {
  37. if ($use && strpos($use, $period[0]) === false) {
  38. continue;
  39. }
  40. $count = floor($seconds / $value);
  41. if ($count == 0 && !$zeros) {
  42. continue;
  43. }
  44. $segments[strtolower($period)] = $count;
  45. $seconds = $seconds % $value;
  46. }
  47.  
  48. // Build the string
  49. foreach ($segments as $key => $value) {
  50. if (($key == 'uker' && $value ==1)or($key == 'timer' && $value ==1)) {
  51. $segment_name = substr($key, 0, -1);
  52. } elseif ($key == '&aring;r') {
  53. $segment_name = $key;
  54. } else {
  55. $segment_name = substr($key, 0, -2);
  56. }
  57. $segment = $value . ' ' . $segment_name;
  58. if ($value != 1) {
  59. // $segment .= 's';
  60. $segment .= 'er';
  61. }
  62. $array[] = $segment;
  63. }
  64.  
  65. $str = implode(', ', $array);
  66. return $str;
  67. }
  68. ?>
  69.  
  70. <?php
  71. echo time_duration($sekunder);
  72. // echo time_duration(100000000, null, true);
  73. // echo time_duration(100000000, 'yMw');
  74. // echo time_duration(100000000, 'd');
  75. ?>
  76.  
  77. // Prints dates ala 3 m���¥neder, 3 uker, 19 timer, 1 minutt

URL: http://aidanlister.com/2004/04/making-time-periods-readable/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.