Gnziet Gnziet - 7 months ago 7
PHP Question

is this correct way to calculate days in php

in above table number of

email
there and
times
i want to add there time in days according to email but this is not working ..

<?php if(count($client)): ?>
<?php $days=0;
date_default_timezone_set('Asia/Kolkata');
?>
<?php foreach ($client as $clents):?>
<?php
if($clents['email']==$clents['email']){
$today = time();
$cdate = strtotime($clents['times']);
$dateDiff = $today - $cdate;
$fullDays = floor($dateDiff/(60*60*24));
$dayscalculate = 30 - $fullDays; // Set number of days ?>

<?php $day=$dayscalculate;
$days +=$day;
}
?>

<tr>
<td><?php echo $clents['id'];?></td>
<td><?php echo $clents['email'];?></td>
<td><?php echo $days.(($days == 1) ? " day" : " days"); ?></td>
<td><?php echo anchor("admin/user_view_data/{$clents['email']}",'Send Mail',array('class'=>'btn btn-info'));?></td>
<?php endforeach;?>
<?php endif; ?>


this is not calculate according to email , this added whole value email by email

i have same email id multiple time like
test@test.com
and there time also multiple how to add time according to email and show email id not duplicate entry.

i have no idea please help me . thanks
here is the output:

Answer

i think your trying to calculate something like this is it .

if mm@gmail.com days =2; again mm@gmail.com days =5;

you need both of mm@gmail.com days =7;

try this

<?php if(count($client)): ?>
  <?php $days=0;
   date_default_timezone_set('Asia/Kolkata');

	$NEW_ARRAY =array();
    ?>
		$i=1;

   <?php foreach ($client as $clents) { ?>
    <?php
		
     if($i==1)
	 {
		  $today = time();
    $cdate = strtotime($clents['times']);
    $dateDiff = $today - $cdate;
    $fullDays = floor($dateDiff/(60*60*24));
    $dayscalculate = 30 - $fullDays; // Set number of days ?>

     <?php $day=$dayscalculate; 
		 
		 $NEW_ARRAY[$clents['email']]=$day
			 
			 //  $days +=$day;
		 
		 
    
   }
	else if(array_key_exists($clents['email'],$NEW_ARRAY))
	{
	
			
   $today = time();
    $cdate = strtotime($clents['times']);
    $dateDiff = $today - $cdate;
    $fullDays = floor($dateDiff/(60*60*24));
    $dayscalculate = 30 - $fullDays; // Set number of days ?>

     <?php $day=$dayscalculate; 
		 
		 $NEW_ARRAY[$clents['email']]=$NEW_ARRAY[$clents['email']]+$day;
			 
		// $days +=$day;
	
	
	}
	else
	{
	
		 $today = time();
    $cdate = strtotime($clents['times']);
    $dateDiff = $today - $cdate;
    $fullDays = floor($dateDiff/(60*60*24));
    $dayscalculate = 30 - $fullDays; // Set number of days ?>

     <?php $day=$dayscalculate; 
		 
		  $NEW_ARRAY[$clents['email']]=$NEW_ARRAY[$clents['email']]+$day;
			 
		// $days +=$day;
	
	
	
	}
	
	$i++;
  }