Simple PHP question...

Discussion in 'Web Design and Development' started by IgnatiusTheKing, Mar 7, 2011.

  1. IgnatiusTheKing macrumors 68040

    IgnatiusTheKing

    Joined:
    Nov 17, 2007
    Location:
    das Fort
    #1
    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...

    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.
     
  2. Laird Knox, Mar 7, 2011
    Last edited by a moderator: Mar 7, 2011

    Laird Knox macrumors 68000

    Joined:
    Jun 18, 2010
    #2
    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.
     
  3. Dunmail macrumors regular

    Joined:
    Mar 27, 2009
    Location:
    Skipton, UK
    #3
    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.
     
  4. IgnatiusTheKing thread starter macrumors 68040

    IgnatiusTheKing

    Joined:
    Nov 17, 2007
    Location:
    das Fort
    #4
    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"); ?>
     
  5. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #5
    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.
     
  6. IgnatiusTheKing, Mar 8, 2011
    Last edited: Mar 8, 2011

    IgnatiusTheKing thread starter macrumors 68040

    IgnatiusTheKing

    Joined:
    Nov 17, 2007
    Location:
    das Fort
    #6
    That is really strange.

    When I use "Y" I get...
    And when I use "y" I get...
     

    Attached Files:

  7. phantax macrumors member

    Joined:
    Feb 2, 2009
    #7
    Make sure you aren't getting any warning in your log about timezone settings like:

    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.
    
     
  8. ppc_michael, Mar 8, 2011
    Last edited: Mar 8, 2011

    ppc_michael Guest

    ppc_michael

    Joined:
    Apr 26, 2005
    Location:
    Los Angeles, CA
    #8
    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.
    
     

Share This Page