tags:

views:

176

answers:

4

each time i try and pull out a date from row caseOpen in tbl_accident I am greeted by the following error...

Warning: gregoriantojd() expects parameter 1 to be long, string given in on line 19

what could be the problem?

the date in caseOpen has the format dd/mm/YYYY

<div style="paddingz: 5px;">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="0" class="cp">
<?php
// Make a MySQL Connection
mysql_select_db("speedycms") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM tbl_accident ORDER BY id ASC") 
or die(mysql_error());  

// Define $color=1 
$color="1";

// date difference
function dateDiff($dformat, $endDate, $beginDate)
{
    $date_parts1=explode($dformat, $beginDate);
    $date_parts2=explode($dformat, $endDate);
    $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return $end_date - $start_date;
}

$date1 = $row['caseOpen'];
$date2 = date('d/m/Y');

echo '
<tr bgcolor="#cccccc"> 
<td width="45px">Case ID</td> 
<td>Client Name/Address</td> 
<td>Accident Date</td> 
<td>Case Opened</td> 
<td>Days Running</td></tr>';
while($rows=mysql_fetch_array($result)){


// If $color==1 table row color = #FFC600
if($color==1){
echo "<tr bgcolor='#f2f2f2' valign='top'>
<td>".$rows['id']."</td>
<td>".$rows['clientName']." <BR> ".$rows['address']."</td>
<td>".$rows['doaDay']."/".$rows['doaMonth']."/".$rows['doaYear']."</td>
<td>".$rows['caseOpen']."</td>
<td>". dateDiff("/", $date2, $date1) ."</td>
</tr>";
// Set $color==2, for switching to other color 
$color="2";
}

// When $color not equal 1, use this table row color 
else {
echo "<tr bgcolor='#ffffff' valign='top'>
<td>".$rows['id']."</td>
<td>".$rows['clientName']." <BR> ".$rows['address']."</td>
<td>".$rows['doaDay']."/".$rows['doaMonth']."/".$rows['doaYear']."</td>
<td>".$rows['caseOpen']."</td>
</tr>";
// Set $color back to 1 
$color="1";
}

}
echo '';
?>      
</table>      
</div>

i would appreciate any assistance. thanks in advance!

+1  A: 

explode() takes a string and returns an array of strings. use intval() to convert the gregoriantojd() argument(s) to long.

edit

manual:

int gregoriantojd  ( int $month  , int $day  , int $year  )

your code:

explode('d/m/y', ...)
just somebody
thanks for the advice.the error message is gone but now date count is always zero.
jeansymolanza
A: 

Instead of

$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);

try this

$date_parts1=strptime($beginDate, 'dd/mm/YYYY');
$date_parts2=strptime($endDate, 'dd/mm/YYYY');
$start_date=gregoriantojd($date_parts1['tm_mday'], $date_parts1['tm_mon'], $date_parts1['tm_year']);
$end_date=gregoriantojd($date_parts2['tm_mday'], $date_parts2['tm_mon'], $date_parts2['tm_year']);

It's a bit longer, but strptime is specifically for turning formatted date strings into an array of named pieces.

R. Bemrose
Fatal error: Call to undefined function strptime() ??
jeansymolanza
Whoops, I had missed that this isn't implemented on Windows or in versions of PHP prior to 5.1. I had started writing another answer, but I must have canceled it before posting. Hang on and I'll rewrite it from memory.
R. Bemrose
A: 

This is somewhat similar to part of Alix Axel's deleted answer:

function dateDiff($endDate, $beginDate)
{
    $endTimestamp = strtotime($endDate);
    $beginTimestamp = strtotime($beginDate);

    // There are 86400 seconds in a day
    return ($endTimestamp - $beginTimestamp) / 86400;
}

Note that the number of arguments has changed from 3 to 2.

Also note that strtotime makes assumptions on whether 12/11/2009 would be December 11, 2009 or November 12, 2009 based on your locale.

R. Bemrose
thanks for the help. there is no error but date count remains 0. am i using the variables wrong?
jeansymolanza
thanks man managed to get working!
jeansymolanza
I apologize if something was wrong, we don't have PHP where I work, and I don't usually check SO from home. Let me guess, I needed more parentheses on the return line?
R. Bemrose
A: 

this is what i ended up doing R. Bemrose... wanted to skip the whole function thing entirely... what do u think??

$caseOpen = date('d/m/Y', strtotime($rows['caseOpen']));
$caseOpen2 = date('Y-m-d', strtotime($rows['caseOpen']));
$days = (strtotime(date("Y-m-d")) - strtotime("$caseOpen2")) / (60 * 60 * 24);
$days2 = round($days);

it works perfectly!

jeansymolanza