XML List of Dates

Using PHP to generate an XML file containing sequential dates

Page 1 of 1

3 Replies - 614 Views - Last Post: 07 May 2010 - 09:14 AM Rate Topic: -----

#1 cpuguru07  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 21-February 09

XML List of Dates

Posted 07 May 2010 - 06:46 AM

I am using PHP to generate an XML file containing a list of dates from 1/1/2010 to 12/31/2011. However, no matter what date I use for the $start_date, my output always starts at 12/31/1969. My code:

<?php

$document = new DOMDocument();
$document->formatOutput = true; 

$dates_node = $document->appendChild($document->createElement("dates"));

$start_date = mktime(0,0,0,1,1,2010);
$end_date = mktime(0,0,0,12,31,2011);

for ($i = start_date; $i < $end_date; $i = strtotime("+1 Day", $i))
{
	$value = date('m/d/Y', $i);
	
	$date_node = $dates_node->appendChild($document->createElement("date"));
	$value_node = $date_node->appendChild($document->createElement("value"));
	$value = $value_node->appendChild($document->createTextNode($value));
}

$document->save("Dates.xml");

?>



Could you please help me get my output to start at the specified $start_date?

Is This A Good Question/Topic? 0
  • +

Replies To: XML List of Dates

#2 jrm402  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 333
  • Joined: 18-March 10

Re: XML List of Dates

Posted 07 May 2010 - 07:15 AM

Don't worry, your code works fine just a quick fix you forgot a dollar sign:

for ($i = start_date; $i < $end_date; $i = strtotime("+1 Day", $i))



should be
for ($i = $start_date; $i < $end_date; $i = strtotime("+1 Day", $i))



I just echoed the result and you see the errors printing along with it to help you debug it. :)

This post has been edited by jrm402: 07 May 2010 - 07:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 cpuguru07  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 21-February 09

Re: XML List of Dates

Posted 07 May 2010 - 07:27 AM

Thank you so much! Leave it to me to forget a dollar sign...haha.

For future reference, could you tell me exactly what you echoed out and what the error message you referred to was?

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 jrm402  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 333
  • Joined: 18-March 10

Re: XML List of Dates

Posted 07 May 2010 - 09:14 AM

Sure, no problem. Here was the code:
<?php

//$document = new DOMDocument();
//$document->formatOutput = true; 

//$dates_node = $document->appendChild($document->createElement("dates"));

$start_date = mktime(0,0,0,1,1,2010);
$end_date = mktime(0,0,0,12,31,2011);

for ($i = start_date; $i < $end_date; $i = strtotime("+1 Day", $i))
{
        $value = date('m/d/Y', $i);
        
        echo $value . '<br />';
        //$date_node = $dates_node->appendChild($document->createElement("date"));
        //$value_node = $date_node->appendChild($document->createElement("value"));
        //$value = $value_node->appendChild($document->createTextNode($value));
}

//$document->save("Dates.xml");

?>



Exactly the same as your code except I put the echo and commented out the xml building.

The error spit out on the screen was:

Quote

Warning: date() expects parameter 2 to be long, string given in /test.php on line 13

Warning: strtotime() expects parameter 2 to be long, string given in /test.php on line 11


What it means is that $i wasn't set properly because it's missing quotes for a string, set to another variable, or set as an integer and it was set as none. Once you add the dollar sign, then $i is properly set and the function runs properly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1