Archive

Posts Tagged ‘days’

Find The Number Of Days For A Given Month With PHP

October 10th, 2008 No comments

There are two ways to find out the number of days for a given month. The first is to use the date() function in conjunction with the mktime() function to create a date and format this value as the number of days in a given month.

$monthDays = date("t",mktime(0,0,0,12,1,2008));

The second way is to use the function cal_days_in_month(). This function takes three parameters.

  • Canelday: There are a number of different forms of calendar to chose from. The one most English speaking people are interested in is the CAL_GREGORIAN calendar. Take a look at a full list of calendar constands.
  • Month: An integer representing the month (1 – 12).
  • Year: An integer representing the year.

So to find out the number of days in the month of December of 2008 you could use the following.

$monthDays = cal_days_in_month(CAL_GREGORIAN, 12, 2008);

Categories: PHP Tags: , , , , , , ,

Work Out The Number Of Days Before A Date

May 31st, 2008 No comments

Use the following function to work out the number of days between today and a date in the future. The function takes three parameters of the day, month and year of the date in question.

<?php
function dateDifference($day,$month,$year){
 return (int)((mktime (0,0,0,$month,$day,$year) - time(void))/86400);
}
?>

To use this function just give it a date, here are some examples.


echo dateDifference(13,07,2008); // 42
echo dateDifference(01,01,2010); // the next "binary date" = 579
echo dateDifference(25,12,2010); // 937

Categories: PHP Tags: , , , ,