| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Description: same as 1.php, but using the PEAR::Date engine |
|---|
| 4 | * Notice the use of the CALENDAR_ENGINE constant, which |
|---|
| 5 | * switches the calculation "engine" |
|---|
| 6 | * Note: make sure PEAR::Date is a stable release!!! |
|---|
| 7 | */ |
|---|
| 8 | function getmicrotime(){ |
|---|
| 9 | list($usec, $sec) = explode(" ",microtime()); |
|---|
| 10 | return ((float)$usec + (float)$sec); |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | // Switch to PEAR::Date engine |
|---|
| 14 | define('CALENDAR_ENGINE','PearDate'); |
|---|
| 15 | |
|---|
| 16 | if ( !@include 'Calendar/Calendar.php' ) { |
|---|
| 17 | define('CALENDAR_ROOT','../../'); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | if (!isset($_GET['y'])) $_GET['y'] = 2003; |
|---|
| 21 | if (!isset($_GET['m'])) $_GET['m'] = 8; |
|---|
| 22 | if (!isset($_GET['d'])) $_GET['d'] = 9; |
|---|
| 23 | if (!isset($_GET['h'])) $_GET['h'] = 12; |
|---|
| 24 | if (!isset($_GET['i'])) $_GET['i'] = 34; |
|---|
| 25 | if (!isset($_GET['s'])) $_GET['s'] = 46; |
|---|
| 26 | |
|---|
| 27 | switch ( @$_GET['view'] ) { |
|---|
| 28 | default: |
|---|
| 29 | $_GET['view'] = 'calendar_year'; |
|---|
| 30 | case 'calendar_year': |
|---|
| 31 | require_once CALENDAR_ROOT.'Year.php'; |
|---|
| 32 | $c = new Calendar_Year($_GET['y']); |
|---|
| 33 | break; |
|---|
| 34 | case 'calendar_month': |
|---|
| 35 | require_once CALENDAR_ROOT.'Month.php'; |
|---|
| 36 | $c = new Calendar_Month($_GET['y'],$_GET['m']); |
|---|
| 37 | break; |
|---|
| 38 | case 'calendar_day': |
|---|
| 39 | require_once CALENDAR_ROOT.'Day.php'; |
|---|
| 40 | $c = new Calendar_Day($_GET['y'],$_GET['m'],$_GET['d']); |
|---|
| 41 | break; |
|---|
| 42 | case 'calendar_hour': |
|---|
| 43 | require_once CALENDAR_ROOT.'Hour.php'; |
|---|
| 44 | $c = new Calendar_Hour($_GET['y'],$_GET['m'],$_GET['d'],$_GET['h']); |
|---|
| 45 | break; |
|---|
| 46 | case 'calendar_minute': |
|---|
| 47 | require_once CALENDAR_ROOT.'Minute.php'; |
|---|
| 48 | $c = new Calendar_Minute($_GET['y'],$_GET['m'],$_GET['d'],$_GET['h'],$_GET['i']); |
|---|
| 49 | break; |
|---|
| 50 | case 'calendar_second': |
|---|
| 51 | require_once CALENDAR_ROOT.'Second.php'; |
|---|
| 52 | $c = new Calendar_Second($_GET['y'],$_GET['m'],$_GET['d'],$_GET['h'],$_GET['i'],$_GET['s']); |
|---|
| 53 | break; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | // Convert timestamp to human readable date |
|---|
| 57 | $date = new Date($c->getTimestamp()); |
|---|
| 58 | |
|---|
| 59 | echo ( '<h1>Using PEAR::Date engine</h1>' ); |
|---|
| 60 | echo ( 'Viewing: '.@$_GET['view'].'<br />' ); |
|---|
| 61 | echo ( 'The time is now: '.$date->format('%Y %a %e %T').'<br >' ); |
|---|
| 62 | |
|---|
| 63 | $i = 1; |
|---|
| 64 | echo ( '<h1>First Iteration</h1>' ); |
|---|
| 65 | echo ( '<p>The first iteration is more "expensive", the calendar data |
|---|
| 66 | structures having to be built.</p>' ); |
|---|
| 67 | $start = getmicrotime(); |
|---|
| 68 | $c->build(); |
|---|
| 69 | while ( $e = $c->fetch() ) { |
|---|
| 70 | $class = strtolower(get_class($e)); |
|---|
| 71 | $link ="&y=".$e->thisYear()."&m=".$e->thisMonth()."&d=".$e->thisDay(). |
|---|
| 72 | "&h=".$e->thisHour()."&i=".$e->thisMinute()."&s=".$e->thisSecond(); |
|---|
| 73 | $method = 'this'.str_replace('calendar_','',$class); |
|---|
| 74 | echo ( "<a href=\"".$_SERVER['PHP_SELF']."?view=".$class.$link."\">".$e->{$method}()."</a> : " ); |
|---|
| 75 | if ( ($i % 10) == 0 ) { |
|---|
| 76 | echo ( '<br>' ); |
|---|
| 77 | } |
|---|
| 78 | $i++; |
|---|
| 79 | } |
|---|
| 80 | echo ( '<p><b>Took: '.(getmicrotime()-$start).' seconds</b></p>' ); |
|---|
| 81 | |
|---|
| 82 | $i = 1; |
|---|
| 83 | echo ( '<h1>Second Iteration</h1>' ); |
|---|
| 84 | echo ( '<p>This second iteration is faster, the data structures |
|---|
| 85 | being re-used</p>' ); |
|---|
| 86 | $start = getmicrotime(); |
|---|
| 87 | while ( $e = $c->fetch() ) { |
|---|
| 88 | $class = strtolower(get_class($e)); |
|---|
| 89 | $link ="&y=".$e->thisYear()."&m=".$e->thisMonth()."&d=".$e->thisDay(). |
|---|
| 90 | "&h=".$e->thisHour()."&i=".$e->thisMinute()."&s=".$e->thisSecond(); |
|---|
| 91 | $method = 'this'.str_replace('calendar_','',$class); |
|---|
| 92 | echo ( "<a href=\"".$_SERVER['PHP_SELF']."?view=".$class.$link."\">".$e->{$method}()."</a> : " ); |
|---|
| 93 | if ( ($i % 10) == 0 ) { |
|---|
| 94 | echo ( '<br>' ); |
|---|
| 95 | } |
|---|
| 96 | $i++; |
|---|
| 97 | } |
|---|
| 98 | echo ( '<p><b>Took: '.(getmicrotime()-$start).' seconds</b></p>' ); |
|---|
| 99 | ?> |
|---|