Jump to content

php date


henriques

Recommended Posts

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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 />"
)
?>

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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?

Link to comment
Share on other sites


$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');

 

???

Link to comment
Share on other sites

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.  ;)

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites


$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...

Link to comment
Share on other sites


$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 ??

Link to comment
Share on other sites

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)

  :o any candidate? :'(

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.