<?php
/**
* 今日の日時なら時間と分だけに刈る
*
* 2012-01-01 19:14:21 -> 19:14
*
* @param array &$rows
*/
function trimDatetime(&$rows) {
foreach ($rows as &$row) {
if (substr($row['created_at'], 0, 10) === date('Y-m-d')) {
$row['created_at'] = substr($row['created_at'], 11, 5);
}
}
}
$rows = array(
array(
'id' => 1,
'created_at' => date('Y-m-d 19:14:21')
),
array(
'id' => 1,
'created_at' => '2012-01-01 19:14:21'
),
);
echo '[before]' . PHP_EOL;
echo $rows[0]['created_at'] . PHP_EOL;
echo $rows[1]['created_at'] . PHP_EOL;
trimDateTime($rows);
echo '[after]' . PHP_EOL;
echo $rows[0]['created_at'] . PHP_EOL;
echo $rows[1]['created_at'] . PHP_EOL;
結果↓
[before]
2012-09-26 19:14:21
2012-01-01 19:14:21
[after]
19:14
2012-01-01 19:14:21