Saturday, February 24, 2007

Nr of days - Date Function PHP

//
function date_return($days,$start_date=NULL)
{
if ($start_date!="")
{
$start_datex = explode("-",$start_date);
$day=$start_datex[0];
$month=$start_datex[1];
$year=$start_datex[2];
}
else
{
$day=date(d);
$month=date(m);
$year=date(y);
}

$default_month=$month;
while($days>0)
{
$x = cal_days_in_month(CAL_GREGORIAN,$month,$year);
if($default_month==$month)
{
if($day+$days > $x)
{
$days=$days-($x-$day);
$day=0;
if ($month==12)
{
$year++;
$month=01;
}
else
{
$month++;
}
}
else
{
$day=$day+$days;
$days=0;
}
if ($day<10 style="color: rgb(51, 51, 255);">strlen($day)<2){$day="0".$day;} style="color: rgb(51, 51, 255);">strlen($month)<2){$month="0".$month;} style="color: rgb(51, 51, 255);">strlen($year)<2){$year="0".$year;}> $x)
{
$days=$days-($x-$day);
$day=0;
if ($month==12)
{
$year++;
$month=01;
}
else
{
$month++;
}
}
else
{
$day=$days;
$days=0;

}
if ($day<10 style="color: rgb(51, 51, 255);">strlen($day)<2){$day="0".$day;} style="color: rgb(51, 51, 255);">strlen($month)<2){$month="0".$month;} style="color: rgb(51, 51, 255);">strlen($year)<2){$year="0".$year;} date="$day." style="color: rgb(0, 153, 0);">return $date;

}
//


Returns a date after you give it a number of days and a starting date. You can leave the start date blank , and it will give the current date as a start date :).
For example, you can calculate when does a customer have to pay you if he applied for a number of days for your product or service. the function will give you the exact date.

A lot of strings are cut and calculated in order to show a good date, with a 0 in front of a number if the number is smalled than 10.
To call the function you can type:
< ?php $date=
date_return("30"); //will show a date 30 days from now
$date=date_return("30","12-03-2001"); //will show a date 30 days from 12-03-2001
? >
If you wish to sate a starting date, you need to make it exactly like this:
day-month-year, separated by a "-".
That's all!

5 comments:

zbrox said...

There are some CSS style declarations mixed with the code of the function.

cy21 said...

um...yes there are... :D

Anonymous said...

Is it just me or is this function a bit overkill.
Couldn't you just use the mysql function
DATE_ADD('2007-12-08', INTERVAL 10 DAY)

To return the new date.

Anonymous said...

No syndi it isn't overkill - and the reasons are many fold...

(i) A mySQl query may be undesirable for performance reasons
(ii) A sql database may not be available at all.
(iii) The mysql function you provide returns a specific date which may not be the required output - further (php) processing would then be required to format it to X years, X months, X days (for example)
(iv) This list could go on for quite a while and this is infact a perfect solution for a small project i'm on at the moment!
(v) Thank-you author great work!
(vi) See you later code to burn :)
(vii) Posted a anon cos i'm busy - not to hide :)~

Anonymous said...

Still looks like overkill when you could just do:

date('Y-m-d', strtotime('+30 day'));

Or am I missing something?