0

I read about this but does not working for me. Here is my code:

$today = date_create()->format("d/m/Y"); // Today is 25/04/2013
$num_days = GetNumberOfdays();
$end_date = date("d/m/Y", strtotime($today . " + $num_days days")); 

The value that I get from $end_date is 31/12/1969. What am I doing wrong?

2
  • What exactly, is it that you want to get? Commented Apr 25, 2013 at 14:09
  • It looks like your strtotime string needs to be reversed. php.net/manual/en/datetime.formats.relative.php Commented Apr 25, 2013 at 14:13

6 Answers 6

2

Try this instead:

$end_date = date("d/m/Y", strtotime("+ $num_days days", time()));

EDIT: I changed the $today variable to just time() which is essentially getting you the same information if you're just looking for today's date.

Sign up to request clarification or add additional context in comments.

2 Comments

@Overflow012 I updated my answer. Try that out and see if it works for you.
I checked it with var_dump() And I get an int so it is ok.
1

From what it looks like you're trying to do, you don't even need $today (as it defaults to now if date is not supplied), so you could just do eg:

 $end_date = date("d/m/Y", strtotime("+ 5 days")); 
 echo $end_date;

result would be

 30/04/2013

if you want to provide a date, you need the parameters the other way round, as per the manual:

strtotime ( string $time [, int $now = time() ] )

Comments

1

date_create() return a DateTime object.

You could use DateTime::modify method.

Comments

1
$date = new \DateTime(); // Defaults to Today
$num_days = 123;
$date->add(
    new \DateInterval('P' . $num_days . 'D')
);
echo $date->format('d-M-Y');

Comments

0
$today = date_create()->format("d/m/Y"); // Today is 25/04/2013

$num_days = date_create()->format("d");

echo $end_date = date("d/m/Y", strtotime(" + $num_days days")); 

Comments

0
<?
// note change of $today format
$today = date_create()->format("d-m-Y"); // Today is 25-04-2013
$num_days = GetNumberOfdays();
$end_date = date("d/m/Y", strtotime("+" . $num_days . " days", strtotime($today)));
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.