Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

IgnatiusTheKing

macrumors 68040
Original poster
Nov 17, 2007
3,657
2
Texas
I'm still newish to PHP, so I'm sure this will be easy, but I need to come up with a way to automatically output the number of years between now and when a company I'm building a website for started (in 1983).

So...I want the output to be something like...

Rocking the free world for 28 years

Right now I have the following code, but it's outputting "1969" years instead of the correct "28"...

PHP:
<?php echo "Rocking the free world for ".date("Y", strtotime("-1983 years")); ?> years

Any help would be much appreciated.
 
Instead of trying to modify the date why not just use simple math? Something like int = YYYY - 1983

I mean you don't need something like "What happened today in 1983." 1983 will always be constant and you can easily get the four digit year out of the current date. Sure you might have a problem with Y10K, but at least it gives you time to come up with a solution. ;)

Oh and I would put our pre text outside of the PHP call like the post text. It would keep it more consistent.
 
Last edited by a moderator:
Date and time programming is always much harder than it should be :eek:

It's easiest to get everything in to a standard format and the easiest is a Unix timestamp, which is just an integer representing the number of seconds
since midnight on the 1st Jan 1970.

PHP:
$curTime = strtotime('now');
$startTime = strtotime('1983-jan-1'); // or whatever date you want - be careful of US style month/day layout use month names instead of numerals.

$difference = $curTime - $startTime; 
// We now have the number of seconds difference between the two dates, so in steps:
$mins = $difference /60;
$hours = $mins / 60;
$days = $hours /24;
$years = $days /365;

Of course you may want to do some rounding up and/or down on those calculations but it should give you a start. You may also want to add 1 to the final value.
 
Date and time programming is always much harder than it should be :eek:

It's easiest to get everything in to a standard format and the easiest is a Unix timestamp, which is just an integer representing the number of seconds
since midnight on the 1st Jan 1970...<snip>

Thanks for all the help, guys. I tried Dunmail's method but kept ending up with weird numbers like 4.7589083 instead of 28, so I looked around elsewhere and found a little tutorial for finding birthdays. I pared the code down to this and it seems to do what I want it to. It seems a little more complex than what I needed (I tried Laird's simple math thing but just don't know about PHP enough, I guess, to get it to work right, either) but if it works, it works, right?

PHP:
<?php function CalculateAge($BirthDate) {list($Year, $Month, $Day) = explode("/", $BirthDate); $YearDiff = date("Y") - $Year; if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff)){ $YearDiff--; } return $YearDiff; } echo CalculateAge("1983/01/01"); ?>
 
Not sure what the issue is on your end, but
PHP:
<?php echo "Rocking the free world for ".date("y", strtotime("-1983 years")); ?> years
Gives me:
Rocking the free world for 28 years
Note I changed the date format to 'y' so I get 28 instead of 0028.
 
Not sure what the issue is on your end, but
PHP:
<?php echo "Rocking the free world for ".date("y", strtotime("-1983 years")); ?> years
Gives me:

Note I changed the date format to 'y' so I get 28 instead of 0028.

That is really strange.

When I use "Y" I get...
Rocking the free world for 1969 years

And when I use "y" I get...
Rocking the free world for 69 years
 

Attachments

  • Picture 2.png
    Picture 2.png
    11.6 KB · Views: 120
Last edited:
Make sure you aren't getting any warning in your log about timezone settings like:

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier

Just do something simple like this:

Code:
<?php
	date_default_timezone_set("America/New_York");
	$elapsed_years = (date("Y") - 1983);
	echo "Rocking the free world for $elapsed_years years.\r\n";
?>

Output:

Code:
Aluminum:Desktop Dj$ php -q test.php 
Rocking the free world for 28 years.
 
Phantax's method is the most straightforward for calculating the difference between two years for sure.

But if you're trying to imply your age, and you weren't born Jan 1st, this won't always be accurate.

If you're using a more recent version of PHP (>=5.3) the DateTime class has a diff function to do exactly what you want.

Code:
<?php
    $dateNow = new DateTime("now");
    $dateBirthday = new DateTime("February 3, 1983");
    $span = $dateNow->diff($dateBirthday);

    echo "\nRocking for ".$span->y." years.\n";

Code:
Rocking for 28 years.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.