0
0

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 1 year has passed since last update.

laravel mysql 年齢範囲でデータを取得

Posted at

参考
https://qiita.com/Ishiki_/items/3928edcc808b5fc02e1f

birthday フィールド(DATETIME型)から
39歳以上40歳以下のユーザーのみ取得したい

hoge.php
<?php
$age = [
    'young' => 39,
    'old' => 40,
];

$birthday = [
    'before' => Carbon::now()->subYear($age['young'])->format('Y-m-d'),
    'after' => Carbon::now()->subYear($age['old'] + 1)->addDay()->format('Y-m-d')
];

$res = User::query()
    ->whereBetween('birthday',[$birthday['after'],$birthday['before']])
    ->limit(25)
    ->get();//全て取得

foreach ($res as $v) {
    //年齢
    $tmp = new Carbon($v->birthday);
    echo $tmp->age.' 歳<br>';
}

こんな感じ whereBetween を使ってみました。

0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?