Jump to content

Recommended Posts

okay the problem i am having is that it is  subtracting a year somehwere so when it is sposed to read Age:1 it outputs age:0  ???

 

<?php
$months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "Septemeber", "October", "November", "December");

$bdayMonth = $exBday[0];
if ($bdayMonth < 10) { $bdayMonth = ereg_replace("0", "", $bdayMonth); }

$exBday[0] = $months[$bdayMonth];

$birthday = "$exBday[0] $exBday[1], $exBday[2]";

$userAge = $this_year - $exBday[2];
if ($this_month < $exBday[0])
{
$userAge = $userAge - 1;
}
if (($this_month == $exBday[0]) AND ($today < $exBday[1]))
{
$userAge = $userAge - 1;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/134818-solved-dont-understand-birthday-script/
Share on other sites

Well, one logical problem you have is that you're comparing $this_month (which I assume is an int) to $months[$bdayMonth] (which is a string). I haven't looked too much into it, so there may be other issues... but there are a lot of undefined variables in just what you've showed us.

I played with your script, and here was the problem that I ran into:

 

where $this_month is compared to $exBday[0], $exBday[0] is a string which the value is a name of a month, like "June". You need to keep $exBday a number value if you want to compare these variables.

 

I admit, I had to add to your script just to figure this out. If you would supply more code I could probably come up with a solution. Here is what I was working with:

 

<?php
$exBday[0] = 6;
$exBday[1] = 28;
$exBday[2] = 2003;

$months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "Septemeber", "October", "November", "December");

$bdayMonth = $exBday[0];
if ($bdayMonth < 10) { $bdayMonth = ereg_replace("0", "", $bdayMonth); }

$exBday[0] = $months[$bdayMonth];

$birthday = "$exBday[0] $exBday[1], $exBday[2]";

echo "<br>The birthday variable = " . $birthday;
$this_year = 2008;
$this_month = 5;

$userAge = $this_year - $exBday[2];

echo "<br>The variable userAge = " . $userAge;
if ($this_month < $exBday[0])
{
   $userAge = $userAge - 1;
}
if (($this_month == $exBday[0]) AND ($today < $exBday[1]))
{
   $userAge = $userAge - 1;
}
echo "<br>The variable userAge is now = " . $userAge;
?>

THIS WORKS:

 

<?php
$exBday[0] = 11;
$exBday[1] = 29;
$exBday[2] = 2003;

$months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "Septemeber", "October", "November", "December");

$bdayMonth = $exBday[0];
if ($bdayMonth < 10) { $bdayMonth = ereg_replace("0", "", $bdayMonth); }

$exBday2[0] = $months[$bdayMonth];

$birthday = "$exBday2[0] $exBday[1], $exBday[2]";

echo "<br>The birthday variable = " . $birthday;
$this_month = (int) date("n");     // MONTH 1-12
$this_year = (int) date("Y");      // YEAR YYYY
$today = (int) date("j");
echo "<br>$this_month = this_month, and $this_year = this_year, and today is $today";

$userAge = $this_year - $exBday[2];

echo "<br>The variable userAge = " . $userAge;
if ($this_month < $exBday[0])
{
   $userAge = $userAge - 1;
}
if (($this_month == $exBday[0]) && ($today < $exBday[1]))
{
   $userAge = $userAge - 1;
}
echo "<br>The variable userAge is now = " . $userAge;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.