Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wouldn't it be nicer to do these calculations in PHP rather than tiring the MySQL? #11

Open
OzanKurt opened this issue Sep 28, 2022 · 0 comments

Comments

@OzanKurt
Copy link

OzanKurt commented Sep 28, 2022

Wouldn't it be nicer to do these calculations in PHP rather than tiring the MySQL?

     /** 
      * Find items for current year 
      * @param  string $column 
      * @return $this 
      */ 
     public function whereCurrentYear($column = 'created_at') 
     { 
          $now = now()->toDateTimeString();
          $startOfThisYear = Carbon::parse('first day of this year')->toDateTimeString();

          return $this->whereRaw("$column between {$startOfThisYear} and {$now}"); 
     } 

/**
* Find items for current year
* @param string $column
* @return $this
*/
public function whereCurrentYear($column = 'created_at')
{
return $this->whereRaw("$column between date_format(now() ,'%Y-01-01') and now()");
}

SELECT * FROM `candidates` WHERE created_at BETWEEN date_format(now() ,'%Y-01-01') and now();
SELECT * FROM `candidates` WHERE created_at BETWEEN "2022-01-01 00:00:00" and "2022-09-28 23:59:59";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant