Add num days to a preset date?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Add num days to a preset date?

Post 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?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

How do you know it doesn't work
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Where are $this_date and $num_days coming from?
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

So i started wondering whether the webserver's systemtime is correct....
Post Reply