Windows で Linux の up コマンド同様の結果をCUI/GUIで返すプログラム。phpexeのテストで作成したもの。systeminfoコマンドを使っているので入っていないと多分エラーになる(=XP Home)。それからsysteminfoを使う手前ウィンドが表示されるけれど、テストということで突っ込まないで一笑してください。
スポンサードリンク
どちらもダウンロードして使って頂けます。GPLなのでどのようにでもお使いください。CUI版のソースは以下のとおりです。またこの時期の開発はCUIとGUIのコードが違います。
※phpexeにするためにphp4になっています。php5用のコードを流用するためにphp5の関数をエミュレートする部分があります。
ダウンロード
UptimeCUI (659 ダウンロード )
UptimeGUI (676 ダウンロード )
main.php
<?php require_once("uptime.php"); $uptime = new uptime(); print $uptime->getStr(); ?>
uptime.php
<?php class uptime{ /* -------------------------------------------------------- * UPTIME ( php for win32 command user interface ) * -------------------------------------------------------- * Summary: * Display UPTIME on Windows like Linux by PHP. * * Operating environment: * - Windows XP HomeEditon is do not work, use other tools * - Windows XP Professional 32bit Japanese (Ver.0.01a-) * - Windows Server 2003 ?? * - Windows Vista ?? * - Windows 7 Uitimate 32bit Japanese (Ver.0.08a-) * * Known Bugs: * - Do not exist SYSYEMINFO COMMAND when output error on your screen. * * History: * - 2010.09.24 Ver.0.12 Bug fix (Missing result now Hour) * - 2010.09.23 Ver.0.11 Change algorithm * - 2010.09.23 Ver.0.10 Bug fix (Windows7 do not get value) * - 2010.09.23 Ver.0.09 Add command /?, print version information * - 2010.09.23 Ver.0.08a Windows7 32bit enabled * - 2010.09.23 Ver.0.07a Change algorithm * - 2010.09.19 Ver.0.01a first * * Other: * - Microsoft uptime.exe http://support.microsoft.com/kb/232243/ja * Code is Shift-JIS * Copyright 2010 CASEY, GPL */ var $ver = '0.12'; var $str = 'hoge'; function uptime(){ $this->str = $this->getUptime(); } function getStr(){ return $this->str; } function getUptime(){ $arr = $this->getSysteminfo(); foreach($arr as $k){ if(mb_ereg("^システム起動時刻:\s*(\d*) 日、(\d*) 時間、(\d*) 分、(\d*) 秒", $k, $lists)){ // WinXP: システム起動時刻: 0 日、3 時間、7 分、56 秒 return sprintf(" %s up %s days, %02d:%02d", date("H:i:s"), $lists[1], $lists[2], $lists[3]); }else if(mb_ereg("^システム起動時間:\s*(\d*)/(\d*)/(\d*), (\d*):(\d*):(\d*)", $k, $lists)){ // Win7: システム起動時刻: yyyy/mm/dd, hh:mm:ss $arr = $this->date_diff(mktime($lists[4],$lists[5],$lists[6],$lists[2],$lists[3],$lists[1]), time()); return sprintf(" %s up %s days, %02d:%02d", date("H:i:s"), $arr['day'], $arr['hour'], $arr['minute']); } } } function getSysteminfo(){ //return getCommandResponse('systeminfo | find "システム起動時刻"'); return $this->getCommandResponse('systeminfo'); } function getCommandResponse($cmd){ $arr = array(); $res = null; exec($cmd, $arr, $res); return $arr; } // for PHP4, By CASEY function date_parse($date_str){ $date_str = strtotime($date_str); $r = array( 'year' => date("Y", $date_str), 'month' => date("m", $date_str), 'day' => date("d", $date_str), 'hour' => date("h", $date_str), 'minute'=> date("i", $date_str), 'second'=> date("s", $date_str) ); return $r; } // copied by http://php.net/manual/ja/function.date-parse.php function date_diff($d1, $d2){ /* compares two timestamps and returns array with differencies (year, month, day, hour, minute, second) */ //check higher timestamp and switch if neccessary if ($d1 < $d2){ $temp = $d2; $d2 = $d1; $d1 = $temp; } else { $temp = $d1; //temp can be used for day count if required } $d1 = $this->date_parse(date("Y-m-d H:i:s",$d1)); $d2 = $this->date_parse(date("Y-m-d H:i:s",$d2)); //seconds if ($d1['second'] >= $d2['second']){ $diff['second'] = $d1['second'] - $d2['second']; } else { $d1['minute']--; $diff['second'] = 60-$d2['second']+$d1['second']; } //minutes if ($d1['minute'] >= $d2['minute']){ $diff['minute'] = $d1['minute'] - $d2['minute']; } else { $d1['hour']--; $diff['minute'] = 60-$d2['minute']+$d1['minute']; } //hours if ($d1['hour'] >= $d2['hour']){ $diff['hour'] = $d1['hour'] - $d2['hour']; } else { $d1['day']--; $diff['hour'] = 24-$d2['hour']+$d1['hour']; } //days if ($d1['day'] >= $d2['day']){ $diff['day'] = $d1['day'] - $d2['day']; } else { $d1['month']--; $diff['day'] = date("t",$temp)-$d2['day']+$d1['day']; } //months if ($d1['month'] >= $d2['month']){ $diff['month'] = $d1['month'] - $d2['month']; } else { $d1['year']--; $diff['month'] = 12-$d2['month']+$d1['month']; } //years $diff['year'] = $d1['year'] - $d2['year']; return $diff; } } ?>
コメント