Maracles Posted October 28, 2009 Share Posted October 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179414-solved-looping-puzzle-involving-dates/ Share on other sites More sharing options...
DavidAM Posted October 28, 2009 Share Posted October 28, 2009 Assign $strtdate to $newdate BEFORE the loop. Inside the loop: Use the PHP date function to get the day of the week: $DOW = date('D', $newdate); Increment $newdate by 1 day Quote Link to comment https://forums.phpfreaks.com/topic/179414-solved-looping-puzzle-involving-dates/#findComment-946677 Share on other sites More sharing options...
Maracles Posted October 28, 2009 Author Share Posted October 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179414-solved-looping-puzzle-involving-dates/#findComment-946692 Share on other sites More sharing options...
DavidAM Posted October 29, 2009 Share Posted October 29, 2009 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); } Quote Link to comment https://forums.phpfreaks.com/topic/179414-solved-looping-puzzle-involving-dates/#findComment-946705 Share on other sites More sharing options...
Maracles Posted October 29, 2009 Author Share Posted October 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179414-solved-looping-puzzle-involving-dates/#findComment-946716 Share on other sites More sharing options...
DavidAM Posted October 29, 2009 Share Posted October 29, 2009 Cool 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). ... Ok, I'll stop ranting now. I need a drink. Quote Link to comment https://forums.phpfreaks.com/topic/179414-solved-looping-puzzle-involving-dates/#findComment-946729 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.