Jump to content

Recommended Posts

I have a for loop which creates a table consisting of 28 rows:

 

	for ($i=1; $i < 29; $i++) {
		echo '<td>'.$i.'</td>';
		echo '<td>'.$newdate.'</td>';
		echo '<td>'.$days[$b++].'</td>';
		echo '</tr>';
		}

 

The first column is simply a number i.e. '1, 2' so forth.

The second column is a date.

The third column is the day i.e. Mon, Tues Wed.

 

My problem is that I need the date in the date column to increase by one day every time the loop runs, however the first date in the first row must be the same as a variable I have in my database called $strtdate. Whilst I have read how to increase a date by one day I do not know how to do this in a loop where the first date is set by a variable.

 

Furthermore I need the day value in the adjacent column to correspond with the day (currently I am using a silly array just as a filler).

 

Any help would be much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/179414-solved-looping-puzzle-involving-dates/
Share on other sites

Thanks for the reply but i'm not quite sure what you mean. I have just tried to incorporate the $DOW part of the code and get the following error:

 

'Notice: A non well formed numeric value encountered in C:\wamp\www\Sandbox\cxapp\stckprofile.php on line 148'

 

Also, what way would recommend for incrementing the date, having read online there seems to a number of alternate ways.

 

 

I guess I should have asked: What is actually in $strtdate?  You say it is a column in the database, what is the data type of the column?  If it is a mySql DateTime, then it needs to be converted to a unix timestamp.  You can do that in the original query using "SELECT UNIX_TIMESTAMP(date_column_name_here) ..." or use strtotime() in PHP.

 

Then the $DOW code should work.  Note: 'D' in there returns the three character abbreviation, for the long name use 'l' (that's a lower-case L)

 

To increment a unix timestamp add one-day's worth of seconds ( 24 * 60 * 60 )

 

   $newdate = strtotime($strtdate);
   for ($i=1; $i < 29; $i++) {
         echo '<td>'.$i.'</td>';
         // Oh yeah, now it's an integer, so we want for format it
         echo '<td>'. date('m/d/Y', $newdate) .'</td>';
         //echo '<td>'.$days[$b++].'</td>';
         echo '<td>'. date('D', $newdate) . '</td>';
         echo '</tr>';
         $newdate += (24 * 60 * 60);
         }

Thank you very much, I have just added that code and it works perfectly. I've been trying to figure that out for a couple of days so its a big help.

 

I have really learnt too much about dates and times yet and exactly how to calculate and formulate them but wanted to get this sorted before I got that far.

 

 

Will mark solved.

Cool :thumb-up: glad to help!  And thanks for marking it solved.  That helps the next guy.

 

Dates are a pain.  Every environment handles them different.  In unix (and therefore) PHP they are integers, in mySql they look like strings but are always displayed in a strict format and you HAVE TO insert/update/query them in that format.  Some functions apply your TimeZone and some apply the server's timezone, and some just ignore you (they're called blind-dates -- Just Kidding).  When you start working with dates in a particular page pick one or the other (PHP's or mySql's) and be consistent.  Unix dates are easy to work with, because they are integers.  But watch out, day 1 is defined as Jan 1 1970 (or is that Day Zero?).  So, you can't event show my birthday in PHP (well, I think PHP 5 now supports negative dates). ...  :facewall:

 

Ok, I'll stop ranting now.    :intoxicated:  I need a drink.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.