denhamd2 Posted April 4, 2007 Share Posted April 4, 2007 just wondering aswell, I have the month and year stored in a variable $thedate so for example $thedate=042007 is there any way of splitting this up into 2 separate variables, $themonth and $theyear? Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/ Share on other sites More sharing options...
jitesh Posted April 4, 2007 Share Posted April 4, 2007 $thedate=042007 $month = substr($thedate,0,2); $year = substr($thedate,2,4); Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221114 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 You could also do it this way: <?php $thedate='042007'; list ($themonth,$theyear) = explode('-',date('m-Y',strtotime($thedate))); echo $themonth . ' -- ' . $theyear; ?> Ken Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221118 Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 great thanks also finally one last small thing: I have the day stored in a variable $day_num its currently spitting out days 1-9 as "1", "2", etc. is there any way of formatting $day_num to make it 2 digits, ie. "01", "02", etc? Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221122 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 Use the function sprint() <?php for ($day_num = 1;$day_num<10;$day_num++) echo sprintf("%02d",$day_num).'<br>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221125 Share on other sites More sharing options...
jitesh Posted April 4, 2007 Share Posted April 4, 2007 if(strlen($day_num) == 1){ $day_num = "0".$day_num ; } Ken has better idea Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221126 Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 thanks Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221129 Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 actually this seems to give a syntax error, any other ways of doing this? Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221137 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 What give a syntax error? You were given two solutions. Ken Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221139 Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 both i'm getting the unpected "{" error. I'm putting this in a while loop so thats probably why? is there any simple php function that will format a number as 2 digits? Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221171 Share on other sites More sharing options...
trq Posted April 4, 2007 Share Posted April 4, 2007 Post your code! Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221173 Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 heres the part in question: //sets the first day of the month to 1 $day_num = 1; //count up the days, untill we've done all of them in the month while ( $day_num <= $days_in_month ) { echo "<td>$day_num<br><input name=\"$day_num$month$year\" type=\"checkbox\" value=\"yes\"></td>\n"; $day_num++; $day_count++; Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221178 Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 any ideas guys? Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221204 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 Need to see more of your code. You show the start of the loop, but not the end. Ken Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221208 Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 here is the rest of the code: //sets the first day of the month to 1 $day_num = 1; //count up the days, untill we've done all of them in the month while ( $day_num <= $days_in_month ) { echo "<td>$day_num<br><input name=\"$day_num$month$year\" type=\"checkbox\" value=\"yes\"></td>\n"; $day_num++; $day_count++; //Make sure we start a new row every week if ($day_count > 7) { echo "</tr><tr>"; $day_count = 1; } } //Finaly we finish out the table with some blanks if needed while ( $day_count >1 && $day_count <=7 ) { echo "<td> </td>"; $day_count++; } Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221214 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 The code you posted doesn't have any errors in it. We need to see more of your code and the exact error message. Ken Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221225 Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 actually figured it out myself, thanks for all your help :) Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221258 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 What was the problem & solution. This is for others who may read this thread. Ken Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221263 Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 think the problem was it wouldn't work because of the while loop but the solution was to use STR_PAD_LEFT $myday_num = str_pad($day_num, 2, "0", STR_PAD_LEFT); Link to comment https://forums.phpfreaks.com/topic/45549-solved-separating-a-variable-into-2/#findComment-221278 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.