phpでCSSファイルを圧縮転送してサイトの表示を高速化させる方法が紹介されていたのでメモひらめき電球


3 ways to compress CSS files using PHP


サイトのパフォーマンスを上げる、PHPでCSSファイルを圧縮転送する各種方法

以下引用です。


1. ob_start で gzhandler を使う方法
<?php if(extension_loaded('zlib')){ob_start('ob_gzhandler');} header("Content-type: text/css"); ?>
body{ background-color:#000000; }
<?php if(extension_loaded('zlib')){ob_end_flush();}?>

2. 1の方法に、Expiresヘッダーを追加してローカルにキャッシュさせる方法
<?php
ob_start ("ob_gzhandler");
header ("content-type: text/css; charset: UTF-8");
header ("cache-control: must-revalidate");
$offset = 60 * 60 * 24 * 30; // 30 日間
$expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
header ($expire);
$css = "body{ background-color:red }";
echo $css;
ob_end_flush();
?>


3. 複数のCSSファイルを1つにまとめて改行、スペースを取り除いて出力
<?php
header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
/* remove comments */
$buffer = preg_replace('!/*[^*]**+([^/][^*]**+)*/!', '', $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array("rn", "r", "n", "t", ' ', ' ', ' '), '', $buffer);
return $buffer;
}

/* your css files */
include('master.css');
include('typography.css');
include('grid.css');
include('print.css');
include('handheld.css');

ob_end_flush();
?>



phpで日付を扱うことは多々ある。
そこで個人的によく日付の比較は使うので
コピペ用にメモひらめき電球

phpで日付の比較を行う場合は
strtotimeでUnix タイムスタンプにして比較する。

if (strtotime(date('Y-m-d')) >= strtotime('2008-12-25')) {}

日付だけでも比較できるのだがたまにおかしな動作になるときが
あるのでUnix タイムスタンプで日付を比較した方が確実に比較できる。


strtotimeは結構便利なのでテクニックをメモひらめき電球

//一時間前を取得
$today = date('Y-m-d H:i:s');
$getday = date('Y-m-d H:i:s', strtotime('-1 hour ' . $today));

PHPの日付処理で残りの日数を調べる方法もあります。
PHPで2つの日付からあと何日あるか調べる方法