How Do You Put A d.o.b on


<?php
class Details
{
  private $name;
  private $age;

  public function __construct($name, $age)
  {
     $this->name=$name;
     $this->age=$age;
  }
 
  function display()
  {
    print "Your name is : $this->name. ";
    print "Your age is: $this->age. ";
    print "Your d.o.b is 12/3/4567. ";
  }
}

$obj = new Details('Jamie', 13 );
$obj->display();
?>

This is what i’ve came up with using ScallioXTX php code but i had to do editing this is what it was.


<?php
class Details
{
  private $name;
  private $age;

  public function __construct($name, $age)
  {
     $this->name=$name;
     $this->age=$age;
  }
 
  function display()
  {
    print "Your name is : $this->name<br>";
    print "Your age is: $this->age<br>";
  }
}

$obj = new Details('Jamie', 13);
$obj->display();

i just took out the <br> as it kept interfering with it.

ok ive got it to work but im still unsure


<?php
class Details
{
  private $name;
  private $age;
  private $date;

  public function __construct($name, $age, $date)
  {
     $this->name=$name;
     $this->age=$age;
     $this->date=$date;
  }
 
  function display()
  {
    print "Your name is : $this->name. ";
    print "Your age is: $this->age. ";
    print "Your d.o.b is: $this->date. ";
  }
}

$obj = new Details('Jamie', 13, '12/3/4567' );
$obj->display();
?>

That looks OK. :slight_smile:

You don’t need to give both d.o.b. and age. If you have the d.o.b. you can calculate the age :slight_smile:

Hi Jamie.

I thought I’d explain what ScallioXTX meant.


<?php
class User
{
  protected
    $name,
    $dob;
    
  public function __construct($name, $dob){
    $this->name = $name;
    $this->dob = $dob;
  }
  
  public function getAge(){
    
    list($y, $m, $d) = explode('-', $this->dob);
    
    $age = date('Y') - $y;
    
    if( (date('j') - $d < 0) || (date('n') - $m < 0) ){
      $age--;
    }
    
    return $age;
  }
  
  public function display(){
    return 'Hello ' . $this->name . ' you appear to be ' . $this->getAge() . PHP_EOL ;
  }
}

$user = new User('Jamie', '1997-07-22');

echo $user->display();
/*
  Hello Jamie you appear to be 13
*/

echo $user->getAge();
/*
  13
*/

Hopefully, you will be able to see that when we call $user->getAge(), it calculates the age from the DOB you provided when you created the $user object.

If you have any questions just post them here. :slight_smile:

any help with putting a exact time on it

like as in the exact time and i perfected your age calculater see
try it.

PHP code:


<?php
class User
{
  protected
    $name,
    $dob;
    
  public function __construct($name, $dob){
    $this->name = $name;
    $this->dob = $dob;
  }
  
  public function getAge(){
    
    list($y, $m, $d) = explode('-', $this->dob);
    
    $age = date('Y') - $y;
    
    if( (date('j') - $d < 0) || (date('n') - $m < 0) ){
      $age--;
    }
    
    return $age;
  }
  
  public function display(){
    return 'Hello ' . $this->name . ' you appear to be ' . $this->getAge() . PHP_EOL ;
  }
}

$user = new User('Jamie', '1997-07-22');

echo $user->display();
/*
  Hello Jamie you appear to be
*/

Exact time? How do you mean? Can you provide an example of what you would like to see?

Jamie, can you please make sure you wrap your code in [B][noparse]

[/noparse][/B][I]code[/I][B][noparse]

[/noparse][/B] tags please? :wink:

I don’t understand what you mean about the exact time, you’re going to have to provide an example to show me what you need or would like.

:slight_smile:

how do i do that :wink:

Jamie, I see no difference between your code and Anthony’s?

im on about like 2.30.pm

the diffrence is try his then try mine.:slight_smile:

<?php
class User
{
  protected
    $name,
    $dob;
    
  public function __construct($name, $dob){
    $this->name = $name;
    $this->dob = $dob;
  }
  
  public function getAge(){
    
    list($y, $m, $d) = explode('-', $this->dob);
    
    $age = date('Y') - $y;
    
    if( (date('j') - $d < 0) || (date('n') - $m < 0) ){
      $age--;
    }
    
    return $age;
  }
  
  public function display(){
    return 'Hello ' . $this->name . ' you appear to be ' . $this->getAge() . PHP_EOL ;
  }
}

$user = new User('Jamie', '1997-07-22');

echo $user->display();
/*
  Hello Jamie you appear to be
*/

Here’s a simple example using [fphp]date[/fphp] and [fphp]strtotime[/fphp].


<?php
$time = strtotime('1997-07-22 15:36:45');

echo 'Hello Jamie, you were born on ' . date('l \	\\h\\e jS \\of F Y \\a\	 g:i:s a', $time) . PHP_EOL ;

/*
  Hello Jamie, you were born on Tuesday the 22nd of July 1997 at 3:36:45 pm
*/

how did you make it say that using

date('l \	\\h\\e jS \\of F Y \\a\	 g:i:s a', $time) . PHP_EOL ;

Yes, I’ll add some comments for you.


<?php
/*
  Ask the strtotime function to convert
  the '1997-07-22 15:36:45' string into
  a format that PHP can use. (a UNIX timestamp)
*/
$time = strtotime('1997-07-22 15:36:45');

/*
  We then ask the date function to style
  the UNIX timestamp ($time) in the format
  we define as 'l \	\\h\\e jS \\of F Y \\a\	 g:i:s a'
*/
echo 'Hello Jamie, you were born on ' . date('l \	\\h\\e jS \\of F Y \\a\	 g:i:s a', $time) . PHP_EOL ;

/*
  This outputs: "Hello Jamie, you were born on Tuesday the 22nd of July 1997 at 3:36:45 pm"
*/

Try some some simpler things with [fphp]date[/fphp] like…


<?php
$format = 'l';
echo date($format); #Thursday


<?php
$format = 'F';
echo date($format); #April


<?php
$format = 'h:i:s';
echo date($format); #13:53:45


<?php
$format = 'Y';
echo date($format); #2011

Ahhh Ok Thank-You For The Help Anthony And guido You.ve Been A Great Hand. :slight_smile: