1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

datetimeを今日の日付なら時間と分だけにtrimする

Last updated at Posted at 2012-09-26

<?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

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?