[PHP,MySQL]生年月日から年齢を算出する

生年月日から年齢を算出する

■PHP

$birth = '1952-01-26 00:00:00';
$age = (int) ((date('Ymd') - preg_replace('/([\- ]|[0-9]{2}:[0-9]{2}:[0-9]{2})/','',$birth)) / 10000);
echo $age;

結果

65

■MySQL

new_users

id name birth
1 1-名前 1941-01-26 17:30:56
2 2-名前 1952-07-07 00:30:56
3 3-名前 1976-09-02 17:30:56
4 4-名前 1990-05-13 05:00:00
5 5-名前 2016-11-26 17:30:56
6 6-名前 1999-04-01 11:11:11

SQL

SELECT
id,
name,
birth,
DATE_FORMAT(birth, '%Y%m%d') AS tmp_birth,
DATE_FORMAT(NOW(), '%Y%m%d') AS now_date,
FLOOR((DATE_FORMAT(NOW(), '%Y%m%d') - DATE_FORMAT(birth, '%Y%m%d')) / 10000) AS age
FROM new_users
ORDER BY id ASC

結果

id name birth tmp_birth now_date age
1 1-名前 1941-01-26 17:30:56 19410126 20170126 76
2 2-名前 1952-07-07 00:30:56 19520707 20170126 64
3 3-名前 1976-09-02 17:30:56 19760902 20170126 40
4 4-名前 1990-05-13 05:00:00 19900513 20170126 26
5 5-名前 2016-11-26 17:30:56 20161126 20170126 0
6 6-名前 1999-04-01 11:11:11 19990401 20170126 17

[WordPress]ざっくりテンプレ

ざっくりテンプレ

index.php

<!DOCTYPE html>
<html lang="ja">
<?php get_header('Meta'); ?>
<body>
<?php get_header('Top'); ?>

<div id="main" class="clearfix">
<?php get_sidebar('L'); ?>

<div id="content">
	<h2>
		<?php
			if( is_single() ){/* 個別記事 */
				the_title();
			}else if( is_page() ){/* 個別ページ */
				the_title();
			}else if( is_category() ){/* カテゴリーアーカイブ */
				echo 'category : ';
				echo get_cat_name( wp_specialchars($cat,1) );
			}else if( is_search() ){/* 検索結果 */
				echo 'search : ' . wp_specialchars($s, 1);
			}else if( is_archive() ){/* アーカイブ */
				echo 'archive : ' . wp_specialchars($m, 1);
			}else{
				echo 'Home';
			}
		?>
	</h2>

<?php if( is_page() ): ?>	

	<?php if(have_posts()):the_post(); ?>
			<?php if( get_post_status()!='private' && get_post_status()!='pending' ): ?>	
				<?php get_template_part("original-page-$page_id"); ?>
			<?php endif; ?>
	<?php endif; ?>

<?php else: ?>

	<?php if(have_posts()):while(have_posts()):the_post(); ?>
		<?php if( get_post_status()!='private' && get_post_status()!='pending' && !is_page() ): ?>	
			<article>
				<div class="article-inner link-dotted word-break">
					<div class="article-header">
						<a href="<?php the_permalink(); ?>">
							<strong><?php the_title(); ?></strong>
						</a>
						<small class="article-time"><?php the_time('Y年n月j日'); ?></small>
					</div>

					<div class="article-text">
						<?php
							the_content();
						?>
					</div>

					<div class="article-footer clearfix">
						<?php get_template_part('footer-article'); ?>
					</div><!--article-footer-->
				</div><!--article-inner-->
			</article>
		<?php endif; ?>
		<?php endwhile;?>
	<?php else: ?>
			<article>
				<div class="article-inner link-dotted word-break">
					<div class="article-header">
						<img class="article-image" src="<?php echo get_template_directory_uri().'/icon/icon-404.png' ?>"/>
						<strong>
							<?php if (!have_posts() ): ?>
								[Not Found]見つかりませんでした。
							<?php endif; ?>
						</strong>
						<small class="article-time"><?php echo date('Y年m月d日'); ?></small>
					</div>
					<div class="article-text">
						[Not Found]見つかりませんでした。
					</div>
					<div class="article-footer clearfix">
						
					</div><!--article-footer-->
				</div><!--article-inner-->
			</article>
	<?php endif; ?>

<?php endif; ?>

</div><!--content-->

<?php get_sidebar('R'); ?>

</div><!--main-->

<?php get_footer(); ?>

</body>
</html>

header-Meta.php

<head>
	<meta charset="UTF-8" />
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title></title>
	<meta name="author" content="ktyr report" /> 
	<meta name="copyright" content="ktyr report" />
	<meta name="keywords" content="">
	<meta name="description" content="">
	<link href="<?php echo get_stylesheet_directory_uri(); ?>/icon/favicon.ico?t=2" type="image/x-icon" rel="icon" />
	<link href="<?php echo get_stylesheet_directory_uri(); ?>/icon/favicon.ico?t=2" type="image/x-icon" rel="shortcut icon" />
	<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/common/css/common.css" media="screen" />
	<script type="text/javascript" src="<?php echo get_stylesheet_directory_uri().'/common/js/jquery-2.0.3.min.js'; ?>"></script>
</head>

header-Top.php

<header>
	<div class="header-inner">
		<div class="header-inner-inner">
			<h1 id="blog-name"><a href="<?=home_url()?>">wp template</a></h1>
		</div>
	</div>
</header>

sidebar-L.php

<div id="sidebarL">
	<section class="relative">
		<form class="mb-14" role="search" method="get" id="searchform" action="<?=home_url()?>" >
			<input type="search" class="" id="s" name="s" placeholder="Search">
			<span class="search-icon">
				<button type="submit">
					<span class="hidden">検索</span>
				</button>
			</span>
		</form>
	</section>
	<section>
		<select class="w-sidebarL" name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'> 
			<option value="">archives</option> 
			<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?>
		</select>
	</section>
</div>

sidebar-R.php

<div id="sidebarR">
	<div class="roundbox">
	</div>
</div>

footer.php

<footer>
	<div class="footer-inner">powered by <a href="http://wordpress.org/" target="_blank">WordPress</a></div>
</footer>

footer-article.php

<input type="text" value="<?php the_permalink(); ?>" readonly>

original-page-2.php

オリジナルページ2

[Swift]UITableView insertRows

insertRows

self.mainTableView.beginUpdates()
self.data.insert("jack", at: 0)
self.mainTableView.insertRows(at: [IndexPath(row:0, section:0)], with:UITableViewRowAnimation.top)
self.mainTableView.endUpdates()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return self.data.count
}

[MySQL]SUBSTRING

開始位置は1からなので注意

all_test

id name tel zip
1 あいう 09011112222 1112222
2 なにぬ 09033334444 3334444
3 かきく 09055556666 7775555
4 さしす 09077778888 9002144

SQL

SELECT id,name,tel,zip,SUBSTRING(zip,1,3) AS zip1,SUBSTRING(zip,4,4) AS zip2
FROM all_test
ORDER BY id ASC;

結果

id name tel zip zip1 zip2
1 あいう 09011112222 1112222 111 2222
2 なにぬ 09033334444 3334444 333 4444
3 かきく 09055556666 7775555 777 5555
4 さしす 09077778888 9002144 900 2144

[MySQL]FIND_IN_SET

下記2つのテーブルをtelカラムでjoinしたい

all_test

id name tel
1 あいう 09011112222
2 なにぬ 09033334444
3 かきく 09055556666
4 さしす 09077778888

sub_infos

id tel address
1 090-3333-4444 東京都港区赤坂1-2-2
2 090-7777-8888 神奈川県横浜市中区4-32-4

SQL

SELECT a.id,a.name,a.tel,s.id AS sub_id,s.tel AS sub_tel
FROM all_test AS a
LEFT JOIN sub_infos AS s
ON FIND_IN_SET(a.tel,REPLACE(s.tel,'-',''))
ORDER BY id ASC;

結果

id name tel sub_id sub_tel
1 あいう 09011112222
2 なにぬ 09033334444 1 090-3333-4444
3 かきく 09055556666
4 さしす 09077778888 2 090-7777-8888

[Swift]Array Extension #2

Array Extension #2

mutating func switchValue(_ index1:Int,index2:Int){
    let _index1:Int = (index1 < 0) ? self.count + index1 : index1
    let _index2:Int = (index2 < 0) ? self.count + index2 : index2
    let elm = self[_index1]
    self[_index1] = self[_index2]
    self[_index2] = elm
}