I have the following code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DatesController extends Controller
{
//Função que calcula o inicio e o fim da semana a partir do dia
public static function getWeekBeginEnd($day)
{
$day = strtotime($day);
//Pega o dia da semana
$number_of_day = date('w', $day);
intval($number_of_day);
//Começa a contar a semana na segunda
$number_of_day--;
//Clcula o primeiro dia da Semana
if ($number_of_day < 0) {
$begin_week = strtotime('-7 days', $day);
}
else
$begin_week = strtotime('-'.$number_of_day.' days', $day);
// Calcula o ultimo da semana
$end_week = strtotime('+7 days', $begin_week);
//Coloca os dados numa array
$week['begin'] = date('Y-m-d', $begin_week);
$week['end'] = date('Y-m-d', $end_week);
//retorna a array
return $week;
}
public function currentYear()
{
$current_year = date("Y");
return $current_year;
}
public function currentMonth()
{
$current_month = date("m");
return $current_month;
}
public function yearBegin($current_year = currentYear())
{
$year_begin = $current_year."-01-01";
return $year_begin;
}
public function yearEnd($current_year = currentYear())
{
$year_end = $current_year."-12-31";
return $year_end;
}
public function thisMonthBegin($current_year = currentYear(), $current_month = currentMonth())
{
$this_month_begin = $current_year."-".$current_month."-01";
}
public function nextMonth($current_month = currentMonth())
{
$next_month = $current_month + 1;
if (strlen($next_month) == 1) {
$next_month = "0".$next_month;
}
return $next_month;
}
public function nextMonthBegin($current_year = currentYear(), $next_month = nextMonth())
{
$next_month_begin = $current_year."-".$next_month."-01";
return $next_month_begin;
}
public function now()
{
$now = date("Y-m-d H:i");
return $now;
}
public function today()
{
$today = date("Y-m-d");
return $today;
}
public function todayWithoutSeparators()
{
$todaywithoutseparators = date("Ymd");
return $todaywithoutseparators;
}
public function todayTimeStamp($today = today())
{
$today_timestamp = strtotime($today);
return $today_timestamp;
}
public function yesterdayTimeStamp($today_timestamp = todayTimeStamp())
{
$yesterday_timestamp = strtotime("-1 day", $today_timestamp);
return $yesterday_timestamp;
}
public function yesterday($yesterday_timestamp = yesterdayTimeStamp())
{
$yesterday = date("Y-m-d", $yesterday_timestamp);
return $yesterday;
}
public function tomorrowTimestamp($today_timestamp = todayTimeStamp())
{
$tomorrow_timestamp = strtotime("+1 day", $today_timestamp);
return $tomorrow_timestamp;
}
public function tomorrow($tomorrow_timestamp = tomorrowTimestamp())
{
$tomorrow = date("Y-m-d", $tomorrow_timestamp);
return $tomorrow;
}
public function aftomorrowTimestamp($today_timestamp = todayTimeStamp())
{
$aftomorrow_timestamp = strtotime("+2 day", $today_timestamp);
return $aftomorrow_timestamp;
}
public function aftomorrow($aftomorrow_timestamp = aftomorrowTimestamp())
{
$aftomorrow = date("Y-m-d", $aftomorrow_timestamp);
return $aftomorrow;
}
public function twoDaysAgoTimestamp($today_timestamp = todayTimeStamp())
{
$two_days_ago_timestamp = strtotime("+3 day", $today_timestamp);
return $two_days_ago_timestamp;
}
public function twoDaysAgo($two_days_ago_timestamp = twoDaysAgoTimestamp())
{
$two_days_ago = date("Y-m-d", $two_days_ago_timestamp);
return $two_days_ago;
}
}
I'm getting the following error in the last method:
Constant expression contains invalid operationsHow to fix?
Att:
Based on my colleague's answer below, I resolved this:
public static function currentYear()
{
$current_year = date("Y");
return $current_year;
}
public static function currentMonth()
{
$current_month = date("m");
return $current_month;
}
public static function yearBegin()
{
$current_year = $this->currentYear();
$year_begin = $current_year."-01-01";
return $year_begin;
}