Number of days between dates

One day I was just thinking about days spent by me on this earth, so I decided to make a script for the same.

I am writing here the code, just make a new empty php file and copy and paste the script and execute it to find out the days between two dates.

<h1>For finding days lived on earth</h1>

<b>Select Date :</b>

<form name="dob" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

<table width="300px">



<tr>

		<td><select id="dd"  name="dd" title="Day" style="width:50px;">

		<?php		

		for($i=1;$i<=31;$i++)

		{

			$dd = '';

			if(isset($_REQUEST['dd']) && !empty($_REQUEST['dd']))

			{

				$dd = $_REQUEST['dd'];

			}

			$dt = sprintf("%02d",$i);

			$sel = '';

			if($dd==$dt)

			{

				$sel = 'selected="selected"';

			}

			echo '<option value="'.$dt.'" '.$sel.'>'.$i.'</option>';

		}

		?>

		</select>

		</td>

		<td><select id="mm"  name="mm" title="Month" style="width:50px;" >

		<?php

		$month_array = array(1=>'January',2=>'February',3=>'March',4=>'April',5=>'May',6=>'June',7=>'July',8=>'August',9=>'September',10=>'October',11=>'November',12=>'December');

		for($i=1;$i<=12;$i++)

		{

			$mm = '';			

			$sel = '';			

			if(isset($_REQUEST['mm']) && !empty($_REQUEST['mm']))

			{

				$mm = $_REQUEST['mm'];

			}

			if($mm==$month_array[$i])

			{

				$sel = 'selected="selected"';

			}

			echo '<option value="'.$month_array[$i].'" '.$sel.'>'.$month_array[$i].'</option>';

		}

		?>

		</select>

		</td>

		<td>

		<select id="yr"  name="yr" title="Year" style="width:70px;" >

		<?php		

		$current_yr = date('Y'); //get current year

		$start_yr = ($current_yr-100);

		$end_yr = ($current_yr-18);



		for($i=$start_yr;$i<=$end_yr;$i++)

		{

			$yy = '';

			$yr = $i;

			$sel = '';

			if(isset($_REQUEST['yr']) && !empty($_REQUEST['yr']))

			{

				$yy = $_REQUEST['yr'];

			}

			if($yy==$yr)

			{

				$sel = 'selected="selected"';

			}

			echo '<option value="'.$yr.'" '.$sel.'>'.$yr.'</option>';

		}

		?>

		</select>

		</td>

		<tr>

<tr><td colspan="3"><input type="submit" name="Sub" value="Go"/></td></tr>		

</table>

</form>

<?php 

if(isset($_REQUEST['Sub']))

{

	$dt = $_REQUEST['dd'].'-'.$_REQUEST['mm'].'-'.$_REQUEST['yr'];

	$selected_date = strtotime($dt);	

	$tmp = date('d-m-Y',$selected_date);

	echo 'Selected Date :<b>'.$tmp.'</b><br/>';

	echo 'Current Date :<b>'.date('d-m-Y').'</b><br/>';

	$current_time = time(); 	

	$time_span = $current_time-$selected_date;	

	echo 'Number of days till date :<b>'.ceil(abs($time_span / (60*60*24))).'</b>'; //sec * mins * days

}

?>

Hope its fun 🙂

Leave a Reply

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

Scroll to top