Get "Age" of person - script won't work
Posted: Thu Aug 18, 2011 9:07 am
Code: Select all
$dob = "$day.$month.$year";
$bday = new DateTime('$dob');
echo "$bday";
$date = (date('Y-m-d'));
$today = new DateTime($date);
$diff = $today->diff($bday);
printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);If I echo $bday, it shows nothing.
And I get this error which means nothing to me:
[text]Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: Failed to parse time string ($dob) at position 0 ($): Unexpected character' in C:\xampp\phpMyAdmin\sell\includes\signup.inc:12 Stack trace: #0 C:\xampp\phpMyAdmin\sell\includes\signup.inc(12): DateTime->__construct('$dob') #1 C:\xampp\phpMyAdmin\sell\index.php(123): include('C:\xampp\phpMyA...') #2 C:\xampp\phpMyAdmin\sell\index.php(413): getPage() #3 {main} thrown in C:\xampp\phpMyAdmin\sell\includes\signup.inc on line 12[/text]
This works fine:
Code: Select all
<?php
$bday = new DateTime('01.01.2000');
$date = (date('Y-m-d'));
$today = new DateTime($date);
$diff = $today->diff($bday);
printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
?>What am I doing wrong?