tags:

views:

191

answers:

4

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 April 09, 2010

7 days from now is April 16,2010

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.

+4  A: 

Take a look here - http://php.net/manual/en/function.strtotime.php

<?php
// This is what you need for future date from now.
echo date('Y-m-d H:i:s', strtotime("+7 day"));

// This is what you need for future date from specific date.
echo date('Y-m-d H:i:s', strtotime('01/01/2010 +7 day'));
?>
Ivo Sabev
+1. You may not want to `echo strtotime(...)`. Instead, use the returned value inside the date function, such as `echo date('Y-m-d H:i:s', strtotime('-7 days'))`.
Salman A
A: 

You can use mktime with date. (http://php.net/manual/en/function.date.php)

Date gives you the current date. This is better than simply adding/subtracting to a timestamp since it can take into account daylight savings time.

<?php
# this gets you 7 days earlier than the current date
$lastWeek = mktime(0, 0, 0, date("m")  , date("d")-7, date("Y"));
# now pretty-print it out (eg, prints April 2, 2010.)
echo date("F j, Y.", $lastWeek), "\n";
?>
Daniel G
+1  A: 

You will have to look into strtotime(). I'd imagine your final code would look something like this:

$future_date = "April 16,2010";
$seconds = strtotime($future_date) - time();
$days = $seconds /(60 * 60* 24);
echo $days; //Returns "6.0212962962963"
Sam152
"6.0212962962963" - classical example of PHP float precision issue :)
Ivo Sabev
Not really, just the _exact_ number of days represented as a decimal.
Sam152
+1  A: 

If you are using PHP >= 5.2 i strongly suggest you use the new DateTime object, makes working with dates alot easier =)

<?php
$date = new DateTime("2006-12-12");
$date->modify("+7 day");
echo $date->format("Y-m-d");
?>
ChrisR
great answer,,it helps me lot
Sanjay