Jump to content

Recommended Posts

Hi,

I'm trying to write a code to calculate my age. I've got a fully working script below:

<?php
$crntdate = date('d/m/Y');
$birth = "22/05/1988";

$crntsplit = explode("/", $crntdate);
$birthsplit = explode("/", $birth);

$crntdatestamp = mktime(0, 0, 0, $crntsplit[1], $crntsplit[0], $crntsplit[2]);
$birthstamp = mktime(0, 0, 0, $birthsplit[1], $birthsplit[0], $birthsplit[2]);

$seconds = $crntdatestamp - $birthstamp;

$years = floor($seconds / 31536000);

echo $years . " years";
?>

Just wondering if there was a better way of doing it as finding the seconds and dividing it all seems a bit of an odd way round..

Thanks

Link to comment
https://forums.phpfreaks.com/topic/47377-solved-calculate-age/
Share on other sites

I've found a major problem with this method.. It works okay if I want to display my age, but I'm now trying to use a slightly modified version to say how long my site has been running for..

what I was wanting is something like

this site has been running for about 1 year and 3 months

the problem with my code is it can't define months. It will successfully tell me how many months between todays date and the start date, but not for examply

1 year and 3 months

I end up with something ridiculous like

1 year and 43 months
Link to comment
https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231165
Share on other sites

Like I said I've got a code which display's the amount of days between two given date's

 

<?php
function DateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
return $end_date - $start_date;
}
echo "".DateDiff("-",date("Y-m-d"),$RegDatum)."";
?>

 

If you would divide this result by 365 you'd get an approximat age

you could add another clause checking wether your birthdate has been passed or not.

Link to comment
https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231352
Share on other sites

your 1st code do noi work

try

<?php
$crntdate = "18/05/2007";
$birth = "22/05/1988";

$crntsplit = explode("/", $crntdate);
$birthsplit = explode("/", $birth);

$crntdatestamp = mktime(0, 0, 0, $crntsplit[1], $crntsplit[0], $crntsplit[2]);
$birthstamp = mktime(0, 0, 0, $birthsplit[1], $birthsplit[0], $birthsplit[2]);

$seconds = $crntdatestamp - $birthstamp;

$years = floor($seconds / 31536000);

echo $years . " years";
?>

try

<?php
function my_old($birth, $curent = '') {
$curent = $curent ? $curent : date('d/m/Y');
$birth = explode('/',$birth);
$curent = explode('/', $curent);
$days = array(31,31,28,31,30,31,30,31,31,30,31,30,31);
if (($birth[2] % 4 == 0 and $birth[2] % 100 != 0) or $birth[2] % 400 == 0) $days[2] = 29;
if ($birth[0] > $curent[0]) $curent[0] += $days[--$curent[1]];
if ($birth[1] > $curent[1]) {
	$curent[1] += 12;
	$curent[2] --;
}
return array('year' => $curent[2] - $birth[2], 'month' => $curent[1] - $birth[1], 'day' => $curent[0] - $birth[0]);
}

print_r(my_old('22/05/1988'));
?>

Link to comment
https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231653
Share on other sites

here's my code:

function birthdday($crntdate, $birth){
$crntsplit = explode("/", $crntdate);
$birthsplit = explode("/", $birth);

$crntdatestamp = mktime(0, 0, 0, $crntsplit[1], $crntsplit[0], $crntsplit[2]);
$birthstamp = mktime(0, 0, 0, $birthsplit[1], $birthsplit[0], $birthsplit[2]);

$seconds = $crntdatestamp - $birthstamp;

$years = floor($seconds / 31536000);

echo "<strong>Age:</strong> " . $years . " years";
}

then I'm calling the function using:

<?php birthdday(date('d/m/Y'), "22/05/1988"); ?>

It says that I am 18.

I'm not finding any problem with the age...

Link to comment
https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231675
Share on other sites

okay, I've finnally tested the code you gave me sasa.

With a couple of modifications I've got it working perfectly to calculate both my age and how long my site's been running for!

Thanks.

 

here's my code:

<?php
function birthday($view, $birth, $curent = '') {
$crnt = $crnt ? $crnt : date('d/m/Y');
$birth = explode('/',$birth);
$crnt = explode('/', $crnt);
$days = array(31,31,28,31,30,31,30,31,31,30,31,30,31);
if (($birth[2] % 4 == 0 and $birth[2] % 100 != 0) or $birth[2] % 400 == 0) $days[2] = 29;
if ($birth[0] > $crnt[0]) $crnt[0] += $days[--$crnt[1]];
if ($birth[1] > $crnt[1]) {
	$crnt[1] += 12;
	$crnt[2] --;
}
$age = array('year' => $crnt[2] - $birth[2], 'month' => $crnt[1] - $birth[1], 'day' => $crnt[0] - $birth[0]);
if($view == age){
	echo "<strong>Age:</strong> " . $age['year'] . " years";
}elseif($view == site){
	echo $age['year'] . " ";
	if($age['year'] == 1){
		echo "year ";
	}else{
		echo "years ";
	}
	if($age['month'] > 0){
	echo "and "  . $age['month'] . " ";
		if($age['month'] == 1){
			echo "month ";
		}else{
			echo "months ";
		}
	}
}
}
?>

If added some if statements to check if year and month is plural or not and also wether months should be included at all.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-234167
Share on other sites

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.