, ,

How to Calculate Dates in PHP

Posted by

An easy strategy  to calculate the time between two dates in Hypertext Preprocessor is to make use of the difference between unix timestamp.The folowing script explains out.

Calculating the age Based on Birthdate (Calc_age.php)
//set the date to calculate

$day=25; $month=6; $year=2000; //note birth day is required as day month and the year$bdayunix=mktime (0,0,0,$month,$day,$year); //get ts for then$bowunux=time(); //get unix ts for today$age=floor($ageunix /(365*24*60*60)); //conver from years to seconds echo “age is $age; ?>

The script initializes the date to use in calculating the age.In an actual software,it is seemingly that this information might come from an HTML kind.The script begins by calling mktime() to see the time-stamp for the birthday and for the present time:

$bdayunix=mktime (0,0,0,$month,$day,$year);
$nowunix=tome(); //get ts for today

Now that these dates are in the equal format,that you may quite simply subtract them.


Now we’re going to convert the age measured in seconds back to years by means of dividing via the number of seconds in a yr.The results are then rounded down using the ground() perform because a man or woman just isn’t mentioned to be  e.g 20 until the end of twentieth.

$age=floor($ageunix /(365*24*60*60)); //convering from seconds to years

However these approach is flawed somewhat because it is limited by the range of UNIX timestamps(32-bit integers).birth dates are not ideal application for time-stamps,the above example works on all platforms only for people born from 1970 onward.windows cannot manage time-stamps prior to 1970,even then this calculation is not always accurate because it does not allow for leap year as well as might fail if midnight on  the persons birth date.

php script

Leave a Reply

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