Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: Cannot modify header information - headers already sent by (output started at /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php:340) in /home/tetch1987/tetch1987.com/public_html/shittakabull/wp-includes/feed-rss2.php on line 8
PHP – ShitTakaBull by tetch1987 https://tetch1987.com/shittakabull WordPress関連やjQueryなどウェブ制作のテクニックデータベース Tue, 16 Aug 2016 12:05:12 +0000 ja hourly 1 https://wordpress.org/?v=7.1.5 WordPress 固定ページの最上親ページを取得する https://tetch1987.com/shittakabull/web/wordpress-page-most-parent/ https://tetch1987.com/shittakabull/web/wordpress-page-most-parent/#comments Sat, 27 Dec 2014 03:29:33 +0000 http://tetch1987.com/shittakabull/?p=496 WordPressで固定ページの一番上の親を取得したいときがあるかと思います。get_postなどで得られる固定ページの情報には親のIDしかなく、さらにさかのぼった情報はありません。そのため、孫まで行ってしまったときに祖父を言い当てるのには少々工夫が必要です。

functions.phpに関数を作る

先祖をさかのぼり、最も上の親ページを取得する関数を作成し、functions.phpに記載することでテーマ内で利用できます。
以下をテーマの中のfunctions.phpに記載します。

function get_most_parent_pageID($current_id = ''){
	global $post;
	if($current_id == '') $current_id ==$post->ID;  
	$par_id = get_post($current_id)->post_parent;
	$most_par_id = $current_id;
	while($par_id!= 0) :
		$par_post = get_post($par_id);
		$most_par_id = $par_post->ID;
		$par_id = $par_post->post_parent;
	endwhile;
	return $most_par_id;
}

使い方

初期設定は現在の投稿の最上の親、引数にIDを渡せばその投稿の最上の親のIDを取得できます。
現在の固定ページの最上親のIDを取得できます。

$most_post_id = get_most_parent_pageID(); //最上親のIDを取得する
$most_post = get_post($most_post_id); //投稿を取得

投稿オブジェクトで取得する場合

上記の使い方ではIDを返してそのIDから投稿オブジェクトを取得していますが、それをまとめた関数です。

function get_most_parent_page($current_id = ''){
	global $post;
	if($current_id == '') $current_id ==$post->ID; 
	$current_post = get_post($current_id);
	$par_id = $current_post->post_parent;
	while($par_id!= 0) :
		$par_post = get_post($par_id);
		$current_post = $par_post;
		$par_id = $par_post->post_parent;
	endwhile;
	return $current_post;
}

使用例:固定ページ毎に最上の親のスラッグのクラスを入れる

例えばpage.php内でスラッグのクラスを入れてCSSで背景画像などを変化させたい時、子ページの場合は親ページの、孫ページの場合は祖父ページのスラッグを入れたいときがあるかと思います。

<?php 
//関数によりスラッグを取得
$most_par_post = get_most_parent_page();
$most_par_slug = $most_par_post->post_name; ?>
<div id="page" class="<?php echo $most_par_slug; ?>">
<!--ここに内容を記述-->
</div>

]]>
https://tetch1987.com/shittakabull/web/wordpress-page-most-parent/feed/ 3
WordPress:検索フォームを設置する https://tetch1987.com/shittakabull/web/wordpress-search-form/ https://tetch1987.com/shittakabull/web/wordpress-search-form/#respond Wed, 05 Nov 2014 09:02:09 +0000 http://tetch1987.com/shittakabull/?p=474 WordPressの検索フォームを設置するためには、決まり事があります。今までは独自に検索フォームを作ってWP_Queryに組み込む方法をしていたのですが、勉強も含めて通常のループで表示できる検索フォームをまとめてみようと思います。
余談ですが、キーワードは1600文字以内と制限されているみたいですね。「Fairly insane upper bound for search string lengths.」と書いてあるように、非常識な文字数ではあります。

検索フォームの設置

まずは検索フォームの設置になります。テンプレートにする場合は、「searchform.php」として保存します。ヘッダーなどのテンプレートにformを書き込んでも動作します。
逆に、Wordpressの検索を動作させるためには以下のような決まり事があります。

  1. 検索キーワードとなるinputの「name」は「s」にする(
    <input type="text" name="s" />
    )
  2. フォームはホームにgetで飛ばす(
    <form method="get" action="<?php echo home_url( '/' ); ?>">
    )
  3. labelをつける

しかし、3)をしなくても動作しているため、必要性が分かりません。環境によっては動かなくなるのでしょうか。
分かりませんが、基本的な検索フォームは以下のようになるかと思います。

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
	<label for="s">キーワード</label>
	<input type="text" value="" name="s" id="s" />
	<input type="submit" value="検索" />
</form>

カテゴリーを制限する

検索をかけるカテゴリーを制限したいときもあるかと思います。例えば、WelCartを利用していて商品カテゴリーのみ検索をかけたいときなどです。

特定のカテゴリー
特定のカテゴリーのみを検索結果に表示する場合です。「value」にはカテゴリーIDを入れます。
<input type="hidden" value="1" name="cat" />
複数のカテゴリー
カテゴリのIDをコンマで区切ることで複数の指定をすることが出来ました。
チェックボックスにしたいと思い、nameを「cat[]」にして配列を果たすとWarningが出てしまいました。やはりチェックボックスにしたい場合は、jQueryで動作させるか、検索結果の際にWP_Queryを使うなどの工夫が必要なのかもしれません。
<input type="hidden" value="1,10" name="cat" />

検索結果の表示

検索フォームの結果は「search.php」が適用されます。もしない場合は「index.php」になります。

]]>
https://tetch1987.com/shittakabull/web/wordpress-search-form/feed/ 0
jQuery + PHPでHTMLに画像アクセスカウンターを設置する https://tetch1987.com/shittakabull/web/access-counter-jquery-php/ https://tetch1987.com/shittakabull/web/access-counter-jquery-php/#respond Tue, 09 Sep 2014 03:52:35 +0000 http://tetch1987.com/shittakabull/?p=417 アクセスカウンターの設置は最近減ってきたかもしれませんが、作ることがあったのでメモ。アクセスカウンターはフリーのcgiなどが落ちていましたが、デザインを決めれたり著作権表示をしなくてくていいので、自作した方が早いと思いました。
今回はHTMLからjQueryのAjax()からPHPを実行させ、あらかじめ用意しておいた画像で+1したアクセス数を出力させる方法です。PHPファイルで表示している場合はそのままPHPで表示させればいいのですが、HTMLファイルの場合は静的ページとなるので、Ajaxを利用してPHPを実行させます。
PHPは動作するけど既にあるHTMLファイルにアクセスカウンターを表示させたい場合に便利かと思います。

必要なファイル

以下のファイルをサーバに準備します。

index.html
アクセスカウンターを表示させるHTMLファイルです。
count.js
AjaxによりPHPを実行させるJavascriptファイルです。
0.gif ~ 9.gif
カウンター表示用の0~9までの画像を用意します。拡張子はgifでなくてもかまいません。
count.txt
カウンターの数値を記録するテキストファイルです。
count.php
カウンターの数値をプラスし、画像の表示を渡すPHPファイルです。

下準備

自分でデザインした0~9までの画像を用意します。
また、テキストエディタでテキストファイルを新規作成し、半角数字で「0」とだけ書いたものを「count.txt」として保存します。数字以外のものが入ると、途中でエラーになるかもしれません。

【HTML】index.html

まずはHTMLの記述です。GoogleからjQueryを読み込み、「count.js」を読み込むように設定します。
また、アクセスカウンターを表示させる「<div id”Counter”></div>」を用意しておきます。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-type" content="text/css" />
<meta http-equiv="Content-Script-type" content="text/javascript" />
<title>jQuery + PHPでHTMLに画像アクセスカウンターを設置する</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="count.js"></script>
</head>
<body>
<div id="Counter"></div>
</body>

【javascript】count.js

次にjQueryの記述です。PHPを実行し、指定したIDに表示させます。
「TargetID」にHTMLで指定したアクセスカウンターを表示させるタグのIDを指定してください。
また、PHPファイルのパスをしています。

$(function(){
TargetID = 'Counter';//---------------------------HTMLで指定したカウンターを表示するIDを指定
$.ajax({
	url:'http://domain.com/count.php', //-----読み込むPHPファイルのパスを指定
	success: function(data){
		$("#"+TargetID).prepend(data);
	},
	error: function( data ) {
		$("#"+TargetID).text('エラー');//--エラーの場合の表記
	}
});
});

【PHP】count.php

最後に、PHPの記述です。基本的には設定の部分を変更するだけで大丈夫かと思います。
「count.txt」を読み取ってプラス1させた後、新しいカウントをファイルを書き換えます。
最大桁数で足りない部分は「0」の画像で先に埋め、str_splitによって1文字ずつ配列にしたものを画像として「$html」につき足していきます。

<?php 
/*設定
==================================================*/
$imgBefore = '<img src="http://domain.com/';//--------------------画像ファイルの数字の前まで
$imgAfter = '.gif" />';//-----------------------------------------画像ファイルの数字の後から
$MaxNum = 7;//----------------------------------------------------最大桁数
$countFile = 'count.txt';//---------------------------------------カウント用ファイル
$html = '';//-----------------------------------------------------書き出すHTMLを初期化
/*ファイルの読み込み
==================================================*/
$fp = fopen($countFile, 'r');
$line = fgets($fp);
fclose($fp);
/*カウントのプラス
==================================================*/
$line++;
/*カウントの書き込み
==================================================*/
$fp = fopen($countFile, 'w');
fwrite($fp, $line);
fclose($fp);
/*桁数の足りないところを「0」に
==================================================*/
$numLen = mb_strlen($line, 'UTF-8');//----------------桁数取得
$numRest = $MaxNum - $numLen;//----------------------残りの桁
if($numRest > 0){
	for($i = 0; $i < $numRest; $i++){
		$html .= $imgBefore.'0'.$imgAfter;
	}
}
/*画像にする
==================================================*/
$numArr = str_split($line, 1);//---------------------一文字ずつ分けて配列に
foreach($numArr as $num){
	$html .= $imgBefore.$num.$imgAfter;
}
/*出力
==================================================*/
echo $html;
 ?>

まとめ

以上で完了です。各ファイルのパスやIDにさえ注意すれば表示されると思います。PHPが吐き出すタグの部分をいじれば、「ul」にしたり、クラス指定して背景画像で読み込むようなカスタマイズもできると思います。
更新ボタンを押したりなど、更新すればするだけカウントされてしまうので、キャッシュなどを利用するにはもうちょっといじらないといけないかもしれません。また、直接「count.php」にアクセスしてもカウントされてしまうので、どこから呼び出されたかなどの指定も必要かもしれません。

]]>
https://tetch1987.com/shittakabull/web/access-counter-jquery-php/feed/ 0
時間/日付に関するPHP https://tetch1987.com/shittakabull/web/php-date/ https://tetch1987.com/shittakabull/web/php-date/#respond Fri, 14 Mar 2014 06:47:12 +0000 http://tetch1987.com/shittakabull/?p=336 日付や時間に関する良く使うPHPのまとめです。

現在の時間をタイムスタンプで取得

time()は、現在の時間を取得し、タイムスタンプを返します。タイムスタンプはUnix エポック (1970 年 1 月 1 日 00:00:00 GMT) からの通算秒となるため、データの保存や計算などにはいいですがそのままの値では分かりづらいです。
$today = time();
echo $today;

タイムスタンプを書式化する

date()は、ローカルの日付/時刻を書式化します。以下のフォーマットを入れることで取得できます。
オプションとしてタイムスタンプを指定すると、ファーマットで指定した文字列を返します。デフォルトでは現在の時刻をタイムスタンプで返す「time()」となっているため、タイムスタンプを指定しない場合は現在の日付/時間を書式化することになります。
例:echo date('Y年');は「2014年」のようになります。

フォーマットの文字をエスケープするには「」(バックスラッシュ)を利用します。
例:echo date('the jS');は「the 15th」のようになります。

良く使うフォーマット

日にち
d 01 から 31
j 1 から 31
z 0 から 365(年間)
曜日
D Mon から Sun
l Sunday から Saturday
N 1(月曜日)から 7(日曜日)
w 0 (日曜)から 6 (土曜)
週
W 年単位の週番号
月
F January から December
m 01 から 12
M Jan から Dec
n 1 から 12
t 指定した月の日数
年
L 1なら閏年。0なら閏年ではない。
Y 1999または2003
y 99 または 03
午前・午後
a am または pm
A 午前または午後(大文字)AM または PM
時間
g 1 から 12
G 0 から 23
h 01 から 12
H 00 から 23
分
i 00 から 59
秒
s 00 から 59

タイムスタンプを作成

mktime()は引数に与えられた日付をタイムスタンプに変換します。
引数は、mktime (時間,分,秒,月,日,年)となっています。

$Birth_month = 1;
$Birth_day = 10;
$Birth_year = 1987;
$TimeStamp = mktime(0, 0, 0, $Birth_month, $Birth_day, $Birth_year); //560055030
echo date('Y年m月j日',$TimeStamp) //1987年10月1日と出力されます。

タイムスタンプに変換

YYYYMMDDをstrtotime()で一度タイムスタンプにした後、data()のフォーマットを利用して変換してみます。

$date = 19871001;
echo date('Y年m月d日',strtotime($date));
// 1987年10月01日と出力されます。

現在の時刻を配列で取得

getdate()は現在の時刻を配列にして返します。

print_r(getdate());
/*以下のように出力されます。
Array
(
    [seconds] => 30 //秒
    [minutes] => 10 //分
    [hours] => 6 //時間
    [mday] => 14 //月単位の日
    [wday] => 5 //曜日
    [mon] => 3 //月
    [year] => 2014 //年
    [yday] => 72 //年単位の日
    [weekday] => Friday //曜日(フルスペル)
    [month] => March //月(フルスペル)
    [0] => 1394777430 //time()の戻り値と同じ
)*/

何日後、何年後を取得

mktime()を使った場合、日付が40といった数字になっても翌月に持ち越してくれます。

$today = getdate();
$year = $today['year'];
$month = $today['mon'];
$day = $today['mday'];
// 2年と13ヶ月と40日後
$year = $year+2;
$month = $month+13;
$day = $day+40;
$TimeStamp = mktime(0, 0, 0, $month, $day, $year);
echo date('Y/m/d',$TimeStamp);

strtotime()の引数に入れることでも可能です。
参考URL:http://php-beginner.com/function/datetime/strtotime.html

// 2年と13ヶ月と40日後
echo date('Y/m/d',strtotime("+2 years 13 month 40 days"));

2つの日付の差分で何日何時間何分後か求める

2014.10.06追記
ある日付まで、あと何日と何時間と何分後かを求めています。記事を公開したときには日付までの日/時間/分をそれぞれ求めていましたが、何日と何時間と何分後かを求めるようにしました。やはり数学が不得意だとこういうところが苦手になってくるようです。

$today = time();
$year = 2014;
$month = 10;
$day = 1;
$TargetTime = mktime(0, 0, 0, $month, $day, $year);
$sec = $TargetTime - $today;
$rest_day = floor($rimit_sec/86400);//-------------------------------------指定日までの日数
$rest_hour = floor(($rimit_sec/3600)-($rest_day*24));//--------------------指定日までの時間-日数分
$rest_minite = floor(($rimit_sec/60)-($rest_day*24*60)-($rest_hour*60));//-指定日までの分-時間-日数分
$rest_all_hour = floor($rimit_sec/3600); //--------------------------------指定日までの時間
$rest_all_minite = floor($rimit_sec/60); //--------------------------------指定日までの分
echo $year.'年'.$month.'月'.$day.'日は今から'.$rest_day.'日'.$rest_hour.'時間'.$rest_minite.'分後です。
'; echo $year.'年'.$month.'月'.$day.'日は今から'.$rest_day.'日後で'.$rest_all_hour.'時間後でもあり'.$rest_all_minite.'分後でもあります。
';

日付をチェックする

checkdate()は、渡された日付が妥当かチェックします。引数にはcheckdate(month,day,year)のように利用します。
month:月は 1 から 12 の間となります。
day:日は、指定された month の日数の範囲内になります。year がうるう年の場合は、それも考慮されます。
year:年は 1 から 32767 の間となります。

$year = 2014;
$month = 2;
$day = 29;
if(checkdate($month,$day,$year)) :
	echo $year.'年'.$month.'月'.$day.'日は存在します。';
else :
	echo $year.'年'.$month.'月'.$day.'日は存在しません。';
endif;
// この場合2014年はうるう年ではないため、「2014年2月29日は存在しません。」と出力されます。

]]>
https://tetch1987.com/shittakabull/web/php-date/feed/ 0