Forum Moderators: coopster
I found a tutorialwhich explains how to use the mktime function, but when I try to adapt it to my code, it fails miserably. :(
Page1.php I declare the variables for setting time on an entry using the variables $depyear, $depmonth and $depday. I submit these to page2.php, which displays back the variables as entered into the db. On page3.php I try to list all db entries where the date entered is >= today.
I can get a form of this to work in my tutorial but not when trying to use it in ym own code. Here are the pertinent snippets of my pages:
page1.php:
<select name="depmonth">
<option selected>Select:</option>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
.....etc
<select name="depday">
<option selected>Select:</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
..... etc
<select name="depyear">
<option selected>Select:</option>
<option selected>2005</option>
<option>2006</option>
<option>2007</option></select>
page2.php:
echo '<td>Deposition Date</td>
<td>'.$_POST['depmonth'].' '.$_POST['depday'].', '.$_POST['depyear'].'</td>
</tr>';
page3.php:
$Dmonth = $_POST['depmonth'];
$Dday = $_POST['depday'];
$Dyear = $_POST['depyear'];
if ($Dmonth == 'January') {$r = "1";}
elseif ($Dmonth == 'February') {$r = "2";}
elseif ($Dmonth == 'March') {$r = "3";}
elseif ($Dmonth == 'April') {$r = "4";}
elseif ($Dmonth == 'May') {$r = "5";}
elseif ($Dmonth == 'June') {$r = "6";}
elseif ($Dmonth == 'July') {$r = "7";}
elseif ($Dmonth == 'August') {$r = "8";}
elseif ($Dmonth == 'September') {$r = "9";}
elseif ($Dmonth == 'October') {$r = "10";}
elseif ($Dmonth == 'November') {$r = "11";}
elseif ($Dmonth == 'December') {$r = "12";}
$date1 = mktime(0,0,0, $r, $Dday, $Dyear);
$now = getdate();
$sqlquery = "SELECT depmonth, depday, depyear, diwtitle, id from diw_alpha WHERE $date1 >= NOW()";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query!");
$date1 = $row[0];
$diwtitle = $row[1];
$id = $row[2];
Help!
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query!");
$row = mysql_fetch_row($queryresult);
$depmonth = $row[0];
$depday = $row[1];
$depyear = $row[2];
$diwtitle = $row[3];
$id = $row[4];
Hope that helps. For the variable names I just used your field names. Also just out of curiosity, should:
$sqlquery = "SELECT depmonth, depday, depyear, diwtitle, id from diw_alpha WHERE $date1 >= NOW()";
actually be:
$sqlquery = "SELECT depmonth, depday, depyear, diwtitle, id from diw_alpha WHERE $date1 >= $now");
?
0000-00-00 //2005-03-30
Then with the drop down build the string:
$date_string = $depyear . "-" . $depmonth . "-" . $depday;
$sqlquery = "SELECT * from diw_alpha WHERE date >= '$date_string'";
dc
I am thinking that maybe setting order date like '20050329' instead.. :¦
I was thinking that it would go on page2.php, but that is the page where the depyear, depmonth, and deday variables are set.. So I can't use a standard $sqlquery = "INSERT INTOktime VALUES('','". $_POST['depmonth'] ."','". $_POST['depday'] ."','".$_POST['depyear']."','".$_POST['diwtitle']."','".$_POST['date_string']."')"; function, as the date_string is not set at the same time as the date variables.. Therein lies today's confusion. :p
BadGoat:
First suggestion, use explicit values for your dropdown options, instead of passing the human-readable text:
<option value="03">March</option> <option value="03">3</option> <option value="2005">2005</option> This will make it easier to deal with on many levels.
Second suggestion, use
strtotime() instead of mktime(). Both result in a Unix timestamp, but strtotime() is more supportive of raw input. I've used
strtotime() with all sorts of date syntax, including 20050303, 03-05-2005, Mar 3 2005, and March 03 2005. In support of an earlier suggestion, you are already using
$now=getdate(); to grab the current time ... why not use $now in your query instead of running another time-gathering process? I usually use
date_diff() and time() to compare timestamps against the current time/date, i.e. $timeleft=date_diff(time(),strtotime($thisdate)); If you store the timestamps in your database instead of the human-readable text, it's usually easier to play around with. The timestamp lends itself to easy reconfiguring, where the human-readable text requires lots of conversion (as your code illustrates).
To make a timestamp human-readable with ease:
$readable=strftime("%M %D, %Y",$timestamp); Anyway ... redo your drop down menu code to include explicit values for each option that fit with a convenient time-writing method, and that will help. Then try a couple of the other functions for generating the time variables you need. You may find one that snaps into your app like it was made for it.
<edit>Quick note: there are 86400 seconds in one day and 31536000 seconds in one year. I find these notes to be handy when I'm figuring out if, say, this timestamp is less than 30 days from that timestamp, i.e.
if ($timeleft<86400) { echo "Within 24 hours!"; } On page1.php the variables depyear, depmonth and depday are set. Can I also combine these 3 variables into the $date_string variable on the same page, or must it be done on the next page, page2.php, after the variables have been successfully set in the db? I am reading now about if isset function, but I am not sure if that's the right approach, and if it is, these Googles I am reading are highly uninformative. :p
$date_string = $depyear . "-" . $depmonth . "-" . $depday;
$var1="quick"; $var2="brown"; $var3="fox"; $var4=$var1." ".$var2." ".$var3; or
$firstname=$_POST['user_firstname']; $lastname=$_POST['user_lastname']; $wholename=$firstname." ".$lastname;
name.php:
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$wholename=$firstname." ".lastname;
?>
<form action="name1.php" method="post">
<input type=text name="firstname" size=32>
<input type=text name="lastname" size=32>
<input type="submit" name="Submit" value="Submit" action="required"></FORM>
name1.php:
$sqlquery = "INSERT INTO name VALUES('". $_POST['firstname'] ."','". $_POST['lastname'] ."','".$_POST['wholename']."')";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query!");
$sqlquery = "SELECT * from name ";
echo '<table>
<tr>
<td>Full Name is:</td>
<td>'.$_POST['fullname'].'</td>
</tr>
<Table>';
?>
I think that would be the easiest route, but that would also defeat the purpose of trying to learn how to make two variables become a third variable.
It's tough being a noob. Tutorials only go so far, books are the same, and in my case, taking the time to take a class is out of the question. I have the best luck with forums and the nice people that try to steer me rightways. So far, no one has openly cursed my lack of knowledge.. ;)
Left form stuff on name.php, changed name1.php to this:
<?php
$connect= mysql_connect("localhost","root")
or die("Could not connect to database in localhost!");
$result=mysql_select_db("testdiw")
or die("Could not select that database!");
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$wholename=$firstname." ".$lastname;
$sqlquery = "INSERT INTO name VALUES('". $_POST['firstname'] ."','". $_POST['lastname'] ."','".$wholename."')";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query!");
$sqlquery = "SELECT * from name ";
echo '<table>
<tr>
<td>Full Name is:</td>
<td>'.$wholename.'</td>
</tr>
<Table>';
?>
$firstname=$_POST['firstname']; $lastname=$_POST['lastname']; $wholename=$firstname." ".$lastname; $sqlquery = "INSERT INTO name VALUES('".$firstname."','".$lastname."','".$wholename."')"; Once you have assigned a value to a variable, go ahead and use it. There's no need to keep checking the value of the $_POST[] array if you've done it once and captured the values already.
Rock on! :)
When I do the basic query:
$sqlquery = "SELECT id, diwtitle, date_string from mktime
I get all the db entries, so I know it works. But when I try to narrow it down to only give entries after today I get the ' Could not execute mysql query!' error. The query I tried was:
$sqlquery = "SELECT id, diwtitle, date_string from mktime WHERE $date_string >= CURDATE()";
I have also tried:
$sqlquery = "SELECT id, diwtitle, date_string from mktime WHERE date >= NOW()";
And the various combos of the above. Could someone show me where I am messing up please?
EDIT:::::
NEvermind, I got it..
$sqlquery = "SELECT id, diwtitle, date_string FROM mktime WHERE date_string >= CURDATE() ORDER BY date_string";
:)