0

Im trying to convert 3 variables $day $month $year. Into a date here is my code

$user->age = 23;


$day   = array_rand($days);
$month = array_rand($months);
$year  = date('Y', strtotime('-'.$user->age.' years'));
$date_combine = $day.$month.$year;
$convert = strtotime($date_combine);
$dob = date('d/M/Y', $convert);
dd($dob);

when I output the $dob I just get "01/Jan/1970" when I should be getting "01/Jan/1993". Not sure why this I happening or what Im missing.

Note: Im using laravel 5.1.

kevinabraham
  • 1,378
  • 4
  • 28
  • 55

2 Answers2

2

Try using Carbon (it´s used by Laravel) like this:

$dob = Carbon::createFromDate($year, $month, $day); 

I hope it could help you.

fmgonzalez
  • 823
  • 1
  • 7
  • 17
2

I hope this may help you

$user->age = 23;
$time = strtotime("-$user->age year", time());
echo $date = date("Y-m-d", $time);

Here time() is used for getting current time.

AAT
  • 411
  • 7
  • 16