• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

PHP Syntax Error

What is wrong with the following PHP?
If my url is:
Why does the following not work?
if ((isset($_GET['d']))) {
	$rd = $_GET['d'];
	$requestDate = date($rd, strtotime("+2 weeks"));
	echo '<h1>NEW DATE: '.$requestDate.'</h1>';

Open in new window

it always echo's; 2013-09-08
There is something wrong with the way I am formatting:
$requestDate = date($rd, strtotime("+2 weeks"));
2 Solutions
The first parameter to date() is the date format. So in this case, you're basically saying:
$requestDate = date("2013-09-08", strtotime("+2 weeks"));

Open in new window

Since you don't have any variables like "Y" or "m" in the date format parameter, you end up with the literal string of "2013-09-08" instead of a formatted date value.

I think you are probably thinking more along the lines of:
// Get the timestamp of the parameter passed in
$fromDate = strtotime($rd);  

// Calculate two weeks from that date
$twoWeeks = strtotime("+2 weeks", $fromDate); 

// Generate the YYYY-MM-DD date string for that calculated date
$requestDate = date("Y-m-d", $twoWeeks); 

Open in new window

Hi there,

The explanations given to you by gr8gonzo are right.

However, you can do it in another way, using a DateTime object:
  if ((isset($_GET['d']))) {
    $rd = $_GET['d'];
    $d1 = new DateTime($rd);   // create the DateTime object
    date_modify($d1, "+2 weeks");   // changing the value of the object created
    $requestDate = date_format($d1, "Y-m-d");   // extracting a formatted string 
    echo '<h1>NEW DATE: '.$requestDate.'</h1>'; // publishing the string value

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now