Page 1 of 1

Add num days to a preset date?

Posted: Fri Dec 01, 2006 10:16 am
by lmh85

Code: Select all

function fnc_date_calc($this_date,$num_days,$display){ 
$my_time = strtotime ($this_date); //converts date string to UNIX timestamp 
$timestamp = $my_time + ($num_days * 86400); //calculates # of days passed ($num_days) * # seconds in a day (86400) 
if($display==1){ 
$return_date = date("d-m-Y",$timestamp); //puts the UNIX timestamp back into string format 
}else{ 
$return_date = date("Y-m-d",$timestamp); //puts the UNIX timestamp back into string format 
}
return $return_date;//exit function and return string
}//end of function

i'm having this function.. which adds num days if i call it. It do work fine in my developing machine.. But not my web server.. Is there any one whom can help?

Posted: Fri Dec 01, 2006 10:21 am
by aaronhall
How do you know it doesn't work

Posted: Fri Dec 01, 2006 10:27 am
by lmh85
i did i try out..
The output date in my developing machine is 3-12-2006 whereas when i try it out when i save my stuffs in the web server else where.. It turns out to be 31-3-2007

Posted: Fri Dec 01, 2006 10:50 am
by aaronhall
Where are $this_date and $num_days coming from?

Posted: Fri Dec 01, 2006 11:45 am
by lmh85
when i call this function..

Code: Select all

$displayDate=fnc_date_calc(date("d-m-Y"),2,1);
like the above.. why will it happens? is it because of different php versions?

Posted: Fri Dec 01, 2006 12:01 pm
by John Cartwright

Code: Select all

function fnc_date_calc($date, $days)
{
   return date('d-m-Y', strtotime($date .' + '. $days .' days'));
}
Untested, give it a whirl.

Posted: Sat Dec 02, 2006 1:59 am
by lmh85
hi there.. Thanks for the reply! i had tested it out.. but what it gave me was more 'future' date..
suppose to be 4/12/2006 but it turns out to be 31/5/2008 :(:(:( any more ideas?

Posted: Sat Dec 02, 2006 2:37 am
by volka
Works perfectly fine for me

Code: Select all

<?php
function fnc_date_calc($date, $days)
{
	return date('d-m-Y', strtotime($date .' + '. $days .' days'));
}

for($i=0; $i<31; $i++) {
	echo fnc_date_calc('25-11-2006', $i), "\n";
}
25-11-2006
26-11-2006
27-11-2006
28-11-2006
29-11-2006
30-11-2006
01-12-2006
02-12-2006
03-12-2006
04-12-2006
05-12-2006
06-12-2006
07-12-2006
08-12-2006
09-12-2006
10-12-2006
11-12-2006
12-12-2006
13-12-2006
14-12-2006
15-12-2006
16-12-2006
17-12-2006
18-12-2006
19-12-2006
20-12-2006
21-12-2006
22-12-2006
23-12-2006
24-12-2006
25-12-2006

Posted: Sat Dec 02, 2006 3:45 am
by timvw
So i started wondering whether the webserver's systemtime is correct....