henriques Posted October 31, 2008 Share Posted October 31, 2008 hi all, I need to fill the dates that are missing in a mysql table. eg: If a month is 31 days, must know the dates that are missing in the month. I thought of making a comparison using arrays, which seems to be the right way, but for some reason I'm not able to do it. maybe something like this for ($ a = 1; $ a <date (j), $ a) ( if (strlen ($ a) =='1 ') ($ a = "0 $ to" ) which generates 01 to the date of the current day (today). in other words, must compare two strings and show what is missing. like, array1 = array ( 'a' => 1,2,3,4); array2 = array ( 'b' => 1,3,4); using the code: ? php $ array1 = array ( 'a' =>'1 ', 2','3 ','4'); $ array2 = array ( 'a' =>'1 ','3','4); $ difference = array_diff ($ array1, $ array2); foreach ($ difference as $ k => $ v) ( echo "$ difference [$ k] <br />"; ) result will be 2, which is missing in array2 ?> in other words, must get across the table column (date) put in (array1) generate a code dates of 01 until the present day and put in the (array2) for ($ a = 1; $ a <date (j), $ a) ( if (strlen ($ a) =='1 ') ($ a = "0 $ to" $ d = date ( "Y-m-$ to"); $ s = '$ d', "; $ array2 = array ( 'b' => $ s); ) and compare the two arrays, which shows what dates are missing, then I used the mysql insert to fill out the dates that are missing. I think it is just that. It should be a simple thing to do for those who knows. Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/ Share on other sites More sharing options...
laffin Posted November 1, 2008 Share Posted November 1, 2008 Here ya goes <?php // Get Todays Date list($m,$d,$y)=explode('/','10/17/2008'); //Generate random days schedule $sch=array(); for($counter=intval(floor($d/2));$counter>0;$counter--) { do { $sd = rand(1,$d); } while (array_search($sd,$sch)!=false); $sch[]=$sd; } //Initialize our array $dim=array_fill(1,$d,false); // Fill in our Schedule foreach ($sch as $val) $dim[$val]=true; // Show our Schedule foreach ($dim as $day) echo date('m/d/Y',mktime(0,0,0,$m,$day,$y)). '-' . ($day?'Closed':'Open') ."<BR>\n"; Hope it makes sense to u, a simple array with marks if yer open/closed for those days Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-679864 Share on other sites More sharing options...
henriques Posted November 2, 2008 Author Share Posted November 2, 2008 thanks for responding. yes I did, but now I am having another problem. How to put a variable within an array eg: for($i=1;$i<10;$i++){ $v2 = date("Y-m-$i") } $array2 = array('b' => $v2); print_r($array2); //result gives me 10 i tried with implode, but no success. txs. Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680470 Share on other sites More sharing options...
bobbinsbro Posted November 2, 2008 Share Posted November 2, 2008 this: $v2 = date("Y-m-$i") should be this: $v2[] = date("Y-m-$i") Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680477 Share on other sites More sharing options...
henriques Posted November 2, 2008 Author Share Posted November 2, 2008 this: $v2 = date("Y-m-$i") should be this: $v2[] = date("Y-m-$i") txs it´s not working? how to make this to work? <? $a[] = "'1', '2', '3', '4', '5', '6', '7'"; for($i=1;$i<5;$i++){ $b[] = $i; } $ar1 = array('a' => $a[]); $ar2 = array('b' => $b[]); $dif = array_diff($ar1, $ar2); foreach($dif as $k => $v)( echo "$dif [$k]<br />" ) ?> Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680487 Share on other sites More sharing options...
bobbinsbro Posted November 2, 2008 Share Posted November 2, 2008 $dif = array_diff($ar1['a'], $ar2['b']); this should do what i think you want. Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680488 Share on other sites More sharing options...
henriques Posted November 2, 2008 Author Share Posted November 2, 2008 $dif = array_diff($ar1['a'], $ar2['b']); this should do what i think you want. still not working! i don´t think that the variable $a[] or $b[] its working, it retun nothing with echo or print_r(). $a[] should return '1', '2', '3', '4', '5', '6', '7' $b[] should return '1', '2', '3', '4' the result $dif it should be 5 6 7 Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680489 Share on other sites More sharing options...
henriques Posted November 2, 2008 Author Share Posted November 2, 2008 $dif = array_diff($ar1['a'], $ar2['b']); this should do what i think you want. still not working! i don´t think that the variable $a[] or $b[] its working, it retun nothing with echo or print_r(). $a[] should return '1', '2', '3', '4', '5', '6', '7' $b[] should return '1', '2', '3', '4' the result $dif it should be 5 6 7 Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680490 Share on other sites More sharing options...
bobbinsbro Posted November 2, 2008 Share Posted November 2, 2008 sorry, i didn't read through your code properly first time. this will work: <?php $a[] = array('1', '2', '3', '4', '5', '6', '7'); for($i=1;$i<5;$i++){ $b[] = $i; } $ar1 = array('a' => $a); $ar2 = array('b' => $b); $dif = array_diff($ar1['a'], $ar2['b']); foreach($dif[0] as $k => $v){ echo "$v [$k] <br />"; } ?> this prints the diff values in this format: value [key] Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680502 Share on other sites More sharing options...
PFMaBiSmAd Posted November 2, 2008 Share Posted November 2, 2008 Back to your original question. Why do you want to fill in "missing" dates in a table? If this is for a calendar application, you would generally only store data for dates that have some event associated with them and your application code would just select the data for any range of dates and generate the calendar(s) for the month/year as needed and display the events that exist on any date. What are you trying to accomplish by filling in missing dates? Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680548 Share on other sites More sharing options...
Andy-H Posted November 2, 2008 Share Posted November 2, 2008 $a[] = array('1', '2', '3', '4', '5', '6', '7'); That would make $a a multidimentional array. ie. $a[0] = array ( [0] => 1, [1] => 2, [2] => 3, etc...); so you would haveto access it via $a[0][0]; $a[0][1]; etc.. I think what you meant to write is $a = array('1', '2', '3', '4', '5', '6', '7'); ??? Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680556 Share on other sites More sharing options...
bobbinsbro Posted November 2, 2008 Share Posted November 2, 2008 @andy - whoops. you're right. thanx for the correction. Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680593 Share on other sites More sharing options...
henriques Posted November 3, 2008 Author Share Posted November 3, 2008 Back to your original question...... The reason i need to know the dates they are missing is: I have a hour tracking system, and I need to know the days that the person worked or not. When generating the report, must generate the time and date that the person did not appear to work. so, it can be deduct in the 40 hours weekly worked. Something like that. txs for you reply. Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680844 Share on other sites More sharing options...
henriques Posted November 3, 2008 Author Share Posted November 3, 2008 $a = array('1', '2', '3', '4', '5', '6', '7'); ??? thank you man!! lets make it simple. I have 2 variables: $mike = "'1', '2', '3', '4', '5'"; $nike = " '1', '2', '3'"; How can i put then in array and get the comparation results. Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680849 Share on other sites More sharing options...
Andy-H Posted November 3, 2008 Share Posted November 3, 2008 Why do you have the data stored like that? Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680854 Share on other sites More sharing options...
laffin Posted November 3, 2008 Share Posted November 3, 2008 Ya shudda stuck with the code I gave ya. and just added the routine ya needed ya wudda been done a lot sooner Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-680876 Share on other sites More sharing options...
henriques Posted November 3, 2008 Author Share Posted November 3, 2008 what do you mean? what i really need is put two variables in an array. as in the example above. I thought it was something easy to do, but apparently is not. Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-681001 Share on other sites More sharing options...
henriques Posted November 3, 2008 Author Share Posted November 3, 2008 what do you mean? what i really need is put two variables in an array. as in the example above. I thought it was something easy to do, but apparently is not. Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-681011 Share on other sites More sharing options...
kenrbnsn Posted November 3, 2008 Share Posted November 3, 2008 lets make it simple. I have 2 variables: $mike = "'1', '2', '3', '4', '5'"; $nike = " '1', '2', '3'"; How can i put then in array and get the comparation results. You use the explode() function: <?php $mike = "'1', '2', '3', '4', '5'"; $nike = "'1', '2', '3'"; $ma = explode(',',$mike); $na = explode(',',$nike); echo '<pre>' . print_r($ma,true) . '</pre>'; // the $ma array echo '<pre>' . print_r($na,true) . '</pre>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-681111 Share on other sites More sharing options...
Andy-H Posted November 3, 2008 Share Posted November 3, 2008 $mike = "'1', '2', '3', '4', '5'"; $nike = " '1', '2', '3'"; If the data is stored that way you would need to explode all of the data in the string using $mike = explode(", '", $mike); /*Then remove the first char from the first value in the array, and the last char from the last array value. */ $i = count($mike) - 1; $mike[0] = substr($mike[0], 1); $mike[$i] = substr($mike[$i], 0, -1); /*Repeat this process with the $nike var.*/ $nike = explode("', '", $nike); $n = count($nike) - 1; $nike[0] = substr($nike[0], 1); $nike[$n] = substr($nike[$n], 0, -1); // Now merge the arrays. $combo = array_merge($mike, $nike); With any luck you should now have the array Array ( [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 1 [6] = 2 [7] = 3 ) *** Not tested... Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-681116 Share on other sites More sharing options...
Andy-H Posted November 3, 2008 Share Posted November 3, 2008 Since the data is held in double quotes, wont the apostrophes be counted as part of the string? Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-681118 Share on other sites More sharing options...
Andy-H Posted November 3, 2008 Share Posted November 3, 2008 $mike = "'1', '2', '3', '4', '5'"; $nike = " '1', '2', '3'"; If the data is stored that way you would need to explode all of the data in the string using $mike = explode("', '", $mike); /*Then remove the first char from the first value in the array, and the last char from the last array value. */ $i = count($mike) - 1; $mike[0] = substr($mike[0], 1); $mike[$i] = substr($mike[$i], 0, -1); /*Repeat this process with the $nike var.*/ $nike = explode("', '", $nike); $n = count($nike) - 1; $nike[0] = substr($nike[0], 1); $nike[$n] = substr($nike[$n], 0, -1); // Now merge the arrays. $combo = array_merge($mike, $nike); With any luck you should now have the array Array ( [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 1 [6] = 2 [7] = 3 ) *** Not tested... Missed an apostrophe from the first explode call.. Then the edit button disappeared ??? Just a thaught, $merged = array($mike . ',' . $nike); Maybe that would work too ?? Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-681127 Share on other sites More sharing options...
henriques Posted November 4, 2008 Author Share Posted November 4, 2008 I appreciate all yours efforts but so far nobody solved my problem, does not seem so easy as I thought. I have, $a = '1, 2, 3, 4, 5 ' and $b = '1, 2, 3'; comparing these two variables would have the $result=(4 and 5) I wanted only the result of comparison and not all of the array theory. Simple is that $a e $b outcome (4 and 5) any candidate? :'( Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-681705 Share on other sites More sharing options...
laffin Posted November 4, 2008 Share Posted November 4, 2008 Like I said, if ya stuck with the code I gave ya initially, its not hard getting just the days which are open <?php // Get Todays Date list($m,$d,$y)=explode('/','10/17/2008'); //Generate random days schedule $sch=array(); for($counter=intval(floor($d/2));$counter>0;$counter--) { do { $sd = rand(1,$d); } while (array_search($sd,$sch)!=false); $sch[]=$sd; } //Initialize our array $dim=array_fill(1,$d,false); // Fill in our Schedule foreach ($sch as $val) $dim[$val]=true; // Show our Schedule foreach ($dim as $day) echo date('m/d/Y',mktime(0,0,0,$m,$day,$y)). '-' . ($day?'Closed':'Open') ."<BR>\n"; is the initial code I gave if ya look at the last routine, which is show the schedule, a simple modification, ya can have it build an array of days that are open. $opendates = array() foreach ($dim as $day) if(!$day) $opendates[]=$day; now $opendates has a list of days that is open. I did say, I hope ya understand the code. cuz if ya understood it, it wudn have been difficult to make those modifications. Good Luck Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-681719 Share on other sites More sharing options...
henriques Posted November 6, 2008 Author Share Posted November 6, 2008 I tried to understand the functioning of your code, and as I am novice in php, did not understand much. I think you are complicating a simple answer, I just wanted the difference between (A and B). For you it's easy to make such changes. A = (1, 2, 3); B = (1); $response = missing number 2 and 3 in $B; Without philosophy, pure and simple .... It has a way? If yes, could you help me? txs. Quote Link to comment https://forums.phpfreaks.com/topic/130929-php-date/#findComment-683304 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.