We help IT Professionals succeed at work.

Help me in date interval php

Styleminds
Styleminds asked
on
Hello
I need a php code that help me find a date after a certain days.

Mean i need to know for example after 100 days what will be accurate date , day,month and year if there a ready code it will be much appreciated.
Comment
Watch Question

<?php
$date = date("Y-m-d");// current date

$date1 = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date2 = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date3 = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date4 = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date5 = strtotime(date("Y-m-d", strtotime($date)) . " +100 days");

print date("Y-m-d",$date5);
?>
Avinash ZalaWeb Expert

Commented:
Check the attached function:

Hope this helps
Addy
// $startDate = starting date
// $duration  == number of duration
// $interval == interval type, it can be 'day','month','year'
// $type == by default + , + means will caount future date, - means count past date

function countExpireDate($startDate='', $duration='1', $interval='day',$type='+')
{
	
	$expireDate = '';
	if($startDate=='')
	{
		$startDate = date('Y-m-d H:i:s');
	}
	if($interval=='day')
	{
		$expireDate = strtotime ( $type.$duration."day" , strtotime ($startDate) ) ;
	}
	else if($interval=='month')
	{
		$expireDate = strtotime ( $type.$duration.' month' , strtotime ( $startDate ) ) ;
	}
	else if($interval=='year')
	{
		$expireDate = strtotime ( $type.$duration.' year' , strtotime ( $startDate ) ) ;
	}
	
	$expireDate = date ( 'Y-m-d H:i:s' , $expireDate );	
	return $expireDate;
}

Open in new window

Author

Commented:
sorry i am new in this can you give also the HTML side of this what i need is to put an input that have the days number and i need php to give me the exact date .
 thanks.
Hi,


<?php
if($_POST['submit'])
{
$date = date("Y-m-d");// current date
$month = $_POST['month'];
$week = $_POST['week'];
$day = $_POST['day'];

$date2 = strtotime(date("Y-m-d", strtotime($date)) . " +".$week." week");
$date4 = strtotime(date("Y-m-d", strtotime($date)) . " +".$month." month");
$date5 = strtotime(date("Y-m-d", strtotime($date)) . " +".$day." days");

print "After Days Addition: ".date("Y-m-d",$date5);
print "<br>";
print "After Month Addition: ".date("Y-m-d",$date4);
print "<br>";
print "After Week Addition: ".date("Y-m-d",$date2);
print "<br>";
}
?>
<form action="a.php" method="post">
Month: 
<input type="text" class="text" name="month" value="" size="" maxlength="" /><br>
Week : 
<input type="text" class="text" name="week" value="" size="" maxlength="" /><br>
Days : 
<input type="text" class="text" name="day" value="" size="" maxlength="" /><br>
<input type="submit" class="submit button" name="submit" value="Submit" />
</form>

Open in new window

Save this file as a.php

Author

Commented:
that is not what i need ! what i need is to set numbers of days and the system give me the accurate dates that match this internal of days.
HI,

Could you please explain detail??

Author

Commented:
well it is clear and already stated

i need to know the date of the next 100 days ! that's it i need to put the days and i need to get the exact date
that it
That is doing with the code.

You just need to use the days input field for that.