strange behavior!!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rwahdan
Forum Newbie
Posts: 23
Joined: Thu Feb 12, 2009 1:49 am

strange behavior!!!!

Post by rwahdan »

please take a look at my site:

http://tamouh-salesoffice.com/tamouh/re ... erator.php

try attendance - 1-3-2009 and 3-3-2009 ---- no results
try attendance - 1-3-2009 and 15-3-2009 ---- i get results but 17-3-2009!!!!!

form code
<html>
<head>
<script language="javascript" type="text/javascript" src="datetimepicker.js">

</script>

<script language='javascript'>
function verifyMe(){
var msg='';

if(document.getElementById('types').value==''){
msg+='- Chose Report Type\n\n';}

if(document.getElementById('startdate').value==''){
msg+='- Chose Start Date:\n\n';}

if(document.getElementById('enddate').value==''){
msg+='- Chose End Date:\n\n';}

if(msg!=''){
alert('The following fields are empty or invalid:\n\n'+msg);
return false
}else{
return true }

}
</script>
</head>
<body BGCOLOR="#A5B9C5">
<br><br><br><br><br>
<form name='Generate Report' action='theresults.php' method='POST' onsubmit='return verifyMe();'>
<table align="center" class='table_form_1' id='table_form_1' cellspacing='0'>
<tr>
<td class='ftbl_row_1' ><LABEL for='types' ACCESSKEY='none' ><b style='color:red'>*</b>Chose Report Type
</td>
<td class='ftbl_row_1a' ><select name='types' id='types'>
<option value='Attendance'>Attendance</option>
<option value='Employee Records'>Employee Records</option>
<option value='Helipad'>Helipad</option>
<option value='Events'>Events</option>
<option value='Inventory Usage'>Inventory Usage</option>
<option value='Inventory Quantity'>Inventory Quantity</option>
</select>
</td>
</tr>
<tr>
<td class='ftbl_row_2' ><LABEL for='startdate' ACCESSKEY='none' ><b style='color:red'>*</b>Chose Start Date: - Formate (dd/mm/yyyy)
</td>
<td class='ftbl_row_2a' ><input type='text' name='startdate' id='startdate' size='23' value=''><a href="javascript:NewCal('startdate','ddmmyyyy')"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>

</td>
</tr>
<tr>
<td class='ftbl_row_1' ><LABEL for='enddate' ACCESSKEY='none' ><b style='color:red'>*</b>Chose End Date: - Formate (dd/mm/yyyy)
</td>
<td class='ftbl_row_1a' ><input type='text' name='enddate' id='enddate' size='23' value=''><a href="javascript:NewCal('enddate','ddmmyyyy')"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>

</td>
</tr>
<tr>
<td colspan='2' align='right'><input type='submit' name='submit' value='Submit'>&nbsp;<input type='reset' name='reset' value='Reset'>
</td>
</tr>
</table>
</form>
</body>
</html>
resulsts code
<html>
<body BGCOLOR="#A5B9C5">
</body>
</html>

<?php


$thetype=$_POST['types'];
$thestartdate = $_POST['startdate'];
$thestartdate1 = date('Y-m-d', strtotime($thestartdate));
$theenddate=$_POST['enddate'];
$theenddate1 = date('Y-m-d', strtotime($theenddate));

$host="my.com";
$db_name="mydb";
$username="mydb";
$password="008";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


if ($thetype == "") {

}

elseif ($thetype == "Attendance") {

$sql= "SELECT *,date_format(thedate , '%d/%m/%Y') as thedate FROM attendance WHERE thedate >='$thestartdate1' and thedate <= '$theenddate1'" or die(mysql_error());
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());

if (mysql_num_rows($result) >= 1) {

echo '&nbsp';
echo '<table border="1">';
echo '<tr>';
echo '<th>';
echo 'ID';
echo '</th>';
echo '<th>';
echo 'Name';
echo '</th>';
echo '<th>';
echo 'Position';
echo '</th>';
echo '<th>';
echo 'Emp#';
echo '</th>';
echo '<th>';
echo 'Notes';
echo '</th>';
echo '<th>';
echo 'Date';
echo '</th>';
echo '<th>';
echo 'Day';
echo '</th>';
echo '<th>';
echo 'IN';
echo '</th>';
echo '<th>';
echo 'OUT';
echo '</th>';
echo '<th>';
echo 'BreakHrs';
echo '</th>';
echo '<th>';
echo 'RegularHrs';
echo '</th>';
echo '<th>';
echo 'WorkHrs';
echo '</th>';
echo '<th>';
echo 'Details';
echo '</th>';
echo '</tr>';

while($row = mysql_fetch_assoc($result))
{
echo '<tr><td>'.$row['AttendanceID'].'</td><td>'.$row['thename'].'</td><td>'.$row['theposition'].'</td><td>'.$row['emp#'].'</td><td>'.$row['thenote'].'</td><td>'.$row['thedate'].'</td><td>'.$row['theday'].'</td><td>'.$row['time_in'].'</td><td>'.$row['time_out'].'</td><td>'.$row['break_hrs'].'</td><td>'.$row['reg_hrs'].'</td><td>'.$row['worked_hrs'].'</td><td>'.$row['work_description'].'</td></tr>';
}
echo '</table>';
}
exit;
}
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: strange behavior!!!!

Post by temidayo »

Take a look at the SQL statement:

Code: Select all

 
$sql= "SELECT *,date_format(thedate , '%d/%m/%Y') as thedate FROM attendance WHERE thedate >='$thestartdate1' and thedate <= '$theenddate1'" or die(mysql_error());
It looks like the date format in date_format(thedate , '%d/%m/%Y') differs from $thestartdate1 and $theenddate1.

remember you changed the format for input dates here:

Code: Select all

$thetype=$_POST['types'];
$thestartdate = $_POST['startdate'];
$thestartdate1 = date('Y-m-d', strtotime($thestartdate));
$theenddate=$_POST['enddate'];
$theenddate1 = date('Y-m-d', strtotime($theenddate));
rwahdan
Forum Newbie
Posts: 23
Joined: Thu Feb 12, 2009 1:49 am

Re: strange behavior!!!!

Post by rwahdan »

Yes i did change the format because in mysql the format is yyyy-m-d and i use a datepicker for the user to be user friendly so the format is d-m-Y. so to check database records i have to have yyyy-m-d format, right? so how i can correct this?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: strange behavior!!!!

Post by php_east »

i think if you use unixtime format in the db itself, you can esily manipulate date values. otherwise, you would have to do lots of php and db manipulation.

with unixtime in the db, you can just test if time-dbtime>100 for example very easily.
rwahdan
Forum Newbie
Posts: 23
Joined: Thu Feb 12, 2009 1:49 am

Re: strange behavior! - Updated & found the problem, please help

Post by rwahdan »

i found the problem and i dont know why it is doing this!!!!!!

$thestartdate = $_POST['startdate'];
$thestartdate1 = date("Y-m-d", strtotime($thestartdate));
$theenddate=$_POST['enddate'];
$theenddate1 = date("Y-m-d", strtotime($theenddate));


echo "$thestartdate1 $theenddate1";
echo '<br>';
echo "$thestartdate $theenddate";

the first result is wrong! the second is ok!!!!!! why is that!!!!!

check: http://tamouh-salesoffice.com/tamouh/re ... erator.php
Post Reply