小物関数(misc_function)#3 日付表示

Posted under php by uechoco on 日曜日 9 9月 2007 at 14 : 10 : 00

『[PHP]日付表示のための小さな関数 - Open MagicVox.net』
日付を表示する際に、現在時刻との差を"30分前"や"1年3ヶ月前"のように表示するための小さな関数です。

日付の簡易書式化関数。Smartyのmodifierにしてもいい。普通に使ってもいい。なかなか使える機会もあるのでは?ただし、簡易計算なので、1ヶ月は30日、1年は360日になっている。

ちょっと難点なのが今より前の時間のとき、正しく計算してくれないことだ。オプションは外してしまったが、基準時間も指定できるバージョンに書き換えてみた。php4の資産なので、あまり日持ちしないかもしれないが…。

PHP:
  1. <?php
  2. function time_format($timestamp, $now = null)
  3. {
  4.     $units = array('秒', '分', '時間', '日', 'ヶ月', '年');
  5.  
  6.     if (is_null($now)) $now = time();
  7.     $diff = $timestamp - $now;
  8.     if ($diff <0) {
  9.         $diff = abs($diff);
  10.         $suffix = '前';
  11.     } else {
  12.         $suffix = '後';
  13.     }
  14.  
  15.     if ($diff <60) return sprintf('%d%s%s', $diff, $units[0], $suffix); // sec
  16.     $diff /= 60;
  17.  
  18.     if ($diff <60) return sprintf('%d%s%s', $diff, $units[1], $suffix); // min
  19.     $diff /= 60;
  20.  
  21.     if ($diff <24) return sprintf('%d%s%s', $diff, $units[2], $suffix); // hour
  22.     $diff2 = $diff % 24;
  23.     $diff /= 24;
  24.  
  25.     if ($diff <30)
  26.       if (0 <$diff2)
  27.         return sprintf('%d%s%d%s%s', $diff, $units[3], $diff2, $units[2], $suffix); // day+hour
  28.       else
  29.         return sprintf('%d%s%s', $diff, $units[3], $suffix); // day
  30.     $diff2 = $diff % 30;
  31.     $diff /= 30;
  32.  
  33.     if ($diff <12)
  34.       if (0 <$diff2)
  35.         return sprintf('%d%s%d%s%s', $diff, $units[4], $diff2, $units[3], $suffix); // month+day
  36.       else
  37.         return sprintf('%d%s%s', $diff, $units[4], $suffix); // month
  38.     $diff2 = $diff % 12;
  39.     $diff /= 12;
  40.  
  41.     if (0 <$diff2)
  42.       return sprintf('%d%s%d%s%s', $diff, $units[5], $diff2, $units[4], $suffix); // year+month
  43.     else
  44.       return sprintf('%d%s%s', $diff, $units[5], $suffix); // year
  45. }
  46. ?>

小物関数(misc_function)#2 生年月日から年齢を
http://labs.uechoco.com/blog/2007/08/misc_function2.html

小物関数(misc_function)#1 ファイルサイズ表示調整
http://labs.uechoco.com/blog/2007/08/misc_function1.html


小物関数(misc_function)#2 生年月日から年齢を

Posted under php by uechoco on 火曜日 28 8月 2007 at 00 : 05 : 58

小物関数シリーズ。最近、ITProの生年月日から年齢を計算する簡単な計算式:佐野裕のサーバ管理者日記の記事が好評らしいので関数化しておく。

(今日の日付-誕生日)/10000の小数点以下切捨て

というなんともシンプルな方法。これは革命とも言うべきか。ってなわけで下記が実装コードです。

PHP:
  1. function date2age($birth_unix)
  2. {
  3. $now = date('Ymd');
  4. $birth = date('Ymd', $birth_unix);
  5. return (int)(($now - $birth) / 10000);
  6. }
  7. echo date2age(mktime(0,0,0,8,28,1987));

念のため、2038年問題には対応していない日付計算なので、心配な方はPEAR::Dateなりで、64bit化させてください。


小物関数(misc_function)#1 ファイルサイズ表示調整

Posted under php by uechoco on 月曜日 27 8月 2007 at 23 : 29 : 23

なんとなく色々な小物関数を貯めていこうと思う。まずはファイルサイズを最適な単位で出力する関数。phpにはfilesize関数はあるが、返すのはbytes単位のみ。見た目ですぐにわかるファイルサイズがいいですね。

その1

Webちょいテク:CSSとかPHPとか便利テクあれこれ : ファイル情報の表示

http://sozaifan.exblog.jp/839291/

こちらのブログのソースコードを一部改変したものが下記のソースコード。

PHP:
  1. function get_file_size($filename) {
  2.     if (!file_exists($filename)) {
  3.         return null;
  4.     }
  5.     $size = filesize($filename);
  6.     $sizes = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB');
  7.     $ext = $sizes[0];
  8.     for ($i=1; (($i <count($sizes)) && ($size>= 1024)); $i++) {
  9.         $size = $size / 1024;
  10.         $ext = $sizes[$i];
  11.     }
  12.     return round($size, 2).$ext;
  13. }
  14. echo get_file_size('test.php');

ファイル名を与えると、そのファイルサイズを返す仕組みになっている。

その2

PHP: number_format - Manual

http://php.benscom.com/manual/ja/function.number-format.php#72969

こちらはphpのマニュアルのコメント中にあったソースコード。こちらも少し改変。

PHP:
  1. function bytes($a) {
  2.     $unim = array("Bytes","KB","MB","GB","TB","PB");
  3.     $c = 0;
  4.     while ($a>=1024) {
  5.         $c++;
  6.         $a = $a/1024;
  7.     }
  8.     return number_format($a,($c ? 2 : 0),".",",")." ".$unim[$c];
  9. }
  10. echo bytes(1030);

こちらは、バイト数を与えると書式済みファイルサイズを返してくれるもの。

余談ですが、WindowsAPIではStrFormatByteSize(SHLWAPI.DLL)という関数が同様の処理をしてくれます。phpでも標準で対応して欲しいですね。


Copyright © 2012 うえちょこ@ぼろぐ. WP Theme created by Web Top.