Forum Moderators: coopster

Message Too Old, No Replies

Trying to build a mktime function

to pull time settings from multiple dropdown menus

         

BadGoat

2:37 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



Hi!

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!

dreamcatcher

2:57 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use one of the mysql fetch methods to get your data.

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

?

BadGoat

3:32 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



Hello dc!

Thank you for the answer.. I am trying that next!

As far as I understand, I could use NOW() as it is a MySQL function for this moment.. Similar to CURDATE() which would be even better, but for me, neither of them worked.

gettopreacherman

7:02 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



this happened to me once too...notice when you make the day you have a comma...mktime() doesn't like that when making time from a day like 10 September 2005

if you have September 10, 2005 it will crap out.

BadGoat

7:08 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



OK, I am making absolutely no headway with this all morning. :(

Is there a better way to take three variables which set the date (year, month, day) and make them into a date which can be sorted? I am not out of the noob woods yet for sure, and the more I tinker, the more confused I get.

gettopreacherman

7:25 pm on Mar 30, 2005 (gmt 0)

10+ Year Member




$sqlquery = "SELECT depmonth, depday, depyear, diwtitle, id from diw_alpha WHERE $date1 >= NOW()";

Unless you can post in the future, why would you want >= to NOW?

dreamcatcher

7:27 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of having the date, month and year in seperate db fields, why not try simply having a DATE field in your SQL? This stores date in the format:

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

BadGoat

7:28 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



I have orders in the db which come due at dates in the future.. The variables (depyear, depmonth, and depday) currently can be set with dropdown menus.. But I can't think of a way to be able to sort those in a way that the ones older than today are not shown, but those today or in the future are.

I am thinking that maybe setting order date like '20050329' instead.. :¦

BadGoat

7:32 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



dc, that looks like exactly what I would need.. playing with that, seeing if I can get that to work instead! Would that go on the same script that the date variables are set?

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

StupidScript

7:57 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What fun, eh? :)

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!"; }

</edit>

StupidScript

8:12 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops. Sorry. I've been using
date_diff()
for so long I forgot it's a custom function! :)

Use simple subtraction to figure out the difference between one timestamp and another:

$timediff=($timestamp1-$timestamp2);

Sorry 'bout that.

BadGoat

8:23 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



Thank you kindly stupidscript! Even more information than I had needed, but I see information here which I can use in the near future! Converting my script to the safer /option value=* now!

BadGoat

10:09 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



OK, new question time...

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;

StupidScript

10:58 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can combine anything on any page ... as long as it is available. If your variables are being defined on one page, you can take those definitions and use them to define other variables:

$var1="quick";

$var2="brown";

$var3="fox";

$var4=$var1." ".$var2." ".$var3;

or

$firstname=$_POST['user_firstname'];

$lastname=$_POST['user_lastname'];

$wholename=$firstname." ".$lastname;

BadGoat

2:11 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



OK, stupidscript, I see how that can be done. I tried to do it first with names, and I get the firstname and lastname added to the db, but not the wholename. Below are the two scripts, and I am not sure what I did wrong that the wholename is not added:

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

?>

gettopreacherman

2:35 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



Goat,

just throw the first and last names into a variable then into the database...

$name=Firstname.Lastname

BadGoat

2:43 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



Hi getto,

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

BadGoat

3:19 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



**Resolved**

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

?>

StupidScript

5:43 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pretty close, but still a couple of processes too many:

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

BadGoat

5:49 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



stupidscript: I have a followup question:

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";

:)