954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Getting Future Date

I don't know how to explain this correctly but just some sample for you guys so that you can really get what Im trying to say.

Today is November 28, 2008

3 days from now is December 1,2008

Im looking for a php code, which can give me the exact date giving the number of days interval prior to the current date.

I've been looking for a thread which can solve or even give a hint on how to solve this one but I found none.

xarz
Newbie Poster
24 posts since Nov 2008
Reputation Points: 10
Solved Threads: 1
 

Is it so difficult to use google with keywords search php+get+date+future .
From what I got from this search even me with no interest in PHP could make that part of the code work...

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

I have created a script for you to fiddle with and is below. The below script is a function where you can enter the number of days, months and years untill the future date and the function will return what that date is.

<?
function count_to_date($day,$month,$year)
    {
    $d=date(j)+$day;
    $m=date(n)+$month;
    $y=date(Y)+$year;
    if ($d>31)
        {
        while ($d>31)
            {
            $d-=31;
            $m+=1;
            }
        }
    if ($m>12)
        {
        while ($m>12)
            {
            $m-=12;
            $y+=1;
            }
        }
    //below sets sequence: day/month/year
    $r=$d.'/'.$m.'/'.$y;
    return $r;
    }


//below returns a future date (day/month/year)  
echo count_to_date(3,0,0);

//a bit of theory
//count_to_date(number_of_days,number_of_months,number_of_years);
?>
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

@cwarn23

great! thanks alot for the code..
i haven't tried it yet.. but will let you know if there are any problems with it..

xarz
Newbie Poster
24 posts since Nov 2008
Reputation Points: 10
Solved Threads: 1
 

Hi I really doubt whether this piece of code will work for every date.. It might not work with february date, leap years, and for months with months with 30 days like april..

Im providing you with some other code sample, that can work for most of the cases...

<?php
function count_to_date($curr_day,$curr_month,$curr_yr,$day,$month,$year)
    {
    $d=$curr_day+$day;
    $m=$curr_month+$month;
    $y=$curr_yr+$year;
    if ($m>12)
        {
            $m-=12;
            $y+=1;
        }
	
	$no_of_days=get_days_in_month($m,$y);
	if ($d>$no_of_days)
        {
            $d-=$no_of_days;
            $m+=1;
        }
    
    //below sets sequence: day/month/year
    $r=$d.'/'.$m.'/'.$y;
    return $r;
    }

function leap_year($yr)
{
	if($yr%4==0 || $yr%400==0)
		return true;
	else
		return false;
}
function get_days_in_month($mnth_no,$yr)
{
	switch($mnth_no)
	{
		case 1:
		case 3:
		case 5:
		case 8:
		case 10:
		case 12:return 31;
		break;
		
		case 4:
		case 6:
		case 9:
		case 11:return 30;
		break;
		
		case 2:if(leap_year($yr))
					return 29;
				else
					return 28;
		break;
	}
}
		
//below returns a future date (day/month/year)  
echo count_to_date(30,3,2007,1,0,1);

//a bit of theory
//count_to_date(number_of_days,number_of_months,number_of_years);
?>
sikka_varun
Junior Poster in Training
94 posts since Dec 2008
Reputation Points: 11
Solved Threads: 12
 

sikka_varun and cwarn23


thanks alot for the codes..

I make some modifications to fit my need from your work guys.
basically the changes are adding a parameter so that I can choose what date I want to start counting off.
Here is the outcome:

function count_to_date($sqldate,$day,$month,$year)
{
	$d=substr($sqldate,8,2);
	$m=substr($sqldate,5,2);
	$y=substr($sqldate,0,4);
	$h=substr($sqldate,11,2);
	$min=substr($sqldate,14,2);
	
    $d=intval($d)+$day;
    $m=intval($m)+$month;
    $y=intval($y)+$year;
    if ($d>31)
        {
        while ($d>31)
            {
            $d-=31;
            $m+=1;
            }
        }
    if ($m>12)
        {
        while ($m>12)
            {
            $m-=12;
            $y+=1;
            }
        }
    //below sets sequence: day/month/year
	if($d<10) $d='0'.$d;
	if($m<10) $m='0'.$m;
    $r=$y.'-'.$m.'-'.$d.' '.$h.':'.$min.':00';
    return $r;
}


can be called by

echo count_to_date("2007-07-01 06:00:00",5,0,0);


and the output will be2007-07-06 06:00:00

anyways,thanks alot for the suggestions and the sample code.. i really appreciate it.

xarz
Newbie Poster
24 posts since Nov 2008
Reputation Points: 10
Solved Threads: 1
 

Today is November 28, 2008

3 days from now is December 1,2008

Try below code it will give you future date after 3 days

echo date('l jS F (Y-m-d)', strtotime('+3 days'));
chetan.akarte
Newbie Poster
1 post since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You