Simple PHP Web Script to Keep Up With Your Chickens’ Age PHP Tutorial

Updated: May 7, 2017

Edited Chickager code to make the month estimation more accurate. It no longer rounds up to a whole digit, making the age in months bigger than what it should be (i.e. It would now would say 2.7 months old rather than 3 months old for chickens that are 10.9 weeks old which is clearly not 3 months old yet.)

So, here at the Enders household, we have 4 lively chickens taking up residence in a brooding cage. This is the first set of chickens we’re raising and as of yesterday, they are about a month old. It’s been a lot of fun watching them grow and observing all of the silly things they do. One of my favorite things to watch them do so far is attempting to dust bathe in their pine shavings. They half lay on their side and flail, wallowing in the shavings, sending shavings flying all over the place. It’s pretty hilarious to watch.

Our smallest and most docile chicken sitting on her perch in the brooding cage.

One thing that’s been bugging me is that I keep forgetting what day we got them and thus how old they are exactly (or as close to exactly as I can get.) I have that sort of information stored in a spreadsheet and I update with more information as I go. Below is what that looks like:

A simple spreadsheet that I’ve been using to track the chickens’ approximate age.

Real basic information there and while that’s great and all, I’m kind of getting tired of doing the math to figure out how old they are each time I need that information. I haven’t been sleeping so well the last couple of weeks so retaining that information has been a struggle lately.

There are a handful of reasons why knowing exactly what their age is is very useful including determining at about what temperature their brooding cage needs to be and when to introduce them to the chicken pen outside.

As a side note, the temperature for brooding cages based on their age does have wiggle room. It’s a good rule of thumb to watch their behavior and if they show any signs of it being too warm or cold, to adjust accordingly. (Cold chicks will huddle together under the warming light and chicks that are getting too hot can be seen with their mouths open and/or panting.)

Using and building upon some pre-existing code (including a snippet from Adnan at Stackoverflow), I put together a simple script that does all of the math for me and spits out the chickens’ age for me in terms of days, weeks, and months. You can see it in action where I’ve embedded the code immediately below:

My chicks are:

  • 2831 days old
  • a total of 404.4 weeks old
  • and a whopping 101.1 months old!

As you can see, it simply outputs the information I’m looking for. I didn’t bother to dress it up. I might do that later with CSS. For now, though, it does what I want it to — it tells me approximately how old my chicks are so I don’t have to bother with the math myself. 🙂

The code, in its entirety, follows:

A pretty standard opening for the script and two variables that influence the output that is shown.

<?php

// ChickAger script -- coded by Kim Enders for the purpose of easily keeping track of chickens' age details

// Some variables

$chickName = "My chicks";  // Either the name of your flock or the name of your chicken
$chickAmount = "are"; // Put 'are' for multiple chicks or 'is' for a single chick

The aforementioned snippet from Adnan at Stackoverflow. It finds the exact number of days there are between two dates (in this case, it would be used to find the amount of days between the chicks’ birth date and the current date.)

// This snippet from Stackoverflow user Adnan at http://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates
     $now = strtotime("2017-02-20"); // or your date as well
     $your_date = time();
     $datediff = $your_date - $now;
     $days = floor($datediff/(60*60*24));

We start the script’s output by using the first variables to announce the results. We also open a bulleted list.

// Now display details about your chick's age in days
echo "$chickName $chickAmount: <br />

<ul>
<li>$days days old</li> ";

After checking to make sure that the chick(s) are a week or older, we can check to see how many weeks old they are

// and how many weeks old they are if they're older than 6 days
if($days > 6) {

$weeks = $days/7;
$totalweeks = number_format($weeks, 1); // default value is 1 (to give more precises age), using 0 instead will give a flat number of weeks
         if($totalweeks > 1) { 
         echo "<li>a total of $totalweeks weeks old</li> ";
         } elseif($totalweeks = 1) {
         echo "<li>$totalweeks week old</li>";
         }
}

After checking to make sure that the chick(s) are at least 4 weeks old, we check to see how many months old they are

// lastly, how many months old they are if they're 4 weeks old or older
if($weeks >= 4) {

$months = $weeks/4;
$totalmonths = number_format($months, 1);

       if($totalmonths > 1) {   
  
       echo "<li>and a whopping $totalmonths months old!</li>";

       } elseif ($totalmonths = 1) {

       echo "<li>and a whopping $totalmonths month old!</li>";
       }
}

Then we close the bulleted list and the php itself.

echo "</ul>";

?>

I hope that you found this tutorial useful. I’ve played around with the idea of expanding on it and building a more expansive script to track other chicken-related things. If I end up doing that, you’ll find it in some form here at this blog.

If you found this post useful, leave a comment below!

Leave a Reply

Your email address will not be published. Required fields are marked *