yourbrain Posted February 23, 2006 Share Posted February 23, 2006 I am working on a PHP News manager for my website, it's going well so far - but I decided I'd prefer selecting the date of the posts than have it automatically inserted.I want to be able to select the Month, Day, Year, Hour and Minutes from a drop menu and have that date inserted as [b]article_date[/b].I used the following code to create the menus:[code]<!-- DATETIME Drop Menu --><!-- MONTH --> <select name="month" ><? for($i=2;$i<14;$i++){ $month=date("F",mktime(0,0,0,$i,0,0)); echo "<option value='".($i-1)."'>$month</option>"; } ?></select> <!-- DAY --> <select name="day"><? for($i=1;$i<32;$i++) echo "<option value='$i'>$i</option>"; ?></select>, <!-- YEAR --> <select name="year"><? echo date('Y'); $currYear=date("Y"); for($i=$currYear-6;$i<$currYear+5;$i++) echo "<option value='$i'>$i</option>"; ?></select> <!-- HOUR --> <select name="hour"><? for($i=1;$i<25;$i++) echo "<option value='$i'>$i</option>"; ?></select>:<!-- MINUTES --> <select name="minute"><? for($i=0;$i<60;$i++) echo "<option value='$i'>$i</option>"; ?></select>:<!-- SECONDS --> <select name="second"><? for($i=0;$i<60;$i++) echo "<option value='$i'>$i</option>"; ?></select>$dbDateTime = mktime($_POST['hour'], $_POST['minute'], $_POST['second'], $_POST['month'], $_POST['day'], $_POST['year']);$dbDateTime = date("Y-m-d H:i:s", $dbDateTime);[/code]Now, when I post an article and select a date, the article is displayed with the following datetime: [b]1970-01-01 2:59:59[/b]. I wasn't even born then.Anyway, I am trying to fix this but thought I'd ask, maybe someone here might have a solution. I want my custom date to be the article date.If any of you can help, I'd really appreciate it.Thanks in advance. Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 23, 2006 Share Posted February 23, 2006 welcome to the forums! i hope you find great help here.i'm not positive that this was your intent, but are the last three lines of code you posted running right after you show the select fields? if so, there's your problem. the $_POST variable will only pick up the value of those fields AFTER the form has been submitted.with this in mind, try running your date assignment AFTER your form is submitted, and you should come up with better results. Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 23, 2006 Share Posted February 23, 2006 That's the problem with getting old, we seem to slow down. Anyway here is a quick, rough couple of snippets to assist you.spec.php[code]<html><head></head><body bgcolor="#ffffff" text="#003399" link="#0000ff" vlink="#800080" alink="#ff0000"><form action="spec02.php" method="post"><!-- DATETIME Drop Menu --><!-- MONTH --><select name="month" ><?for($i=1;$i<13;$i++){echo "<option value='".($i)."'>$i</option>";}?></select> <!-- DAY --><select name="day"><?for($i=1;$i<32;$i++)echo "<option value='$i'>$i</option>";?></select>, <!-- YEAR --><select name="year"><?echo date('Y');$currYear=date("Y");for($i=$currYear-6;$i<$currYear+5;$i++)echo "<option value='$i'>$i</option>";?></select> <!-- HOUR --><select name="hour"><?for($i=1;$i<25;$i++)echo "<option value='$i'>$i</option>";?></select>:<!-- MINUTES --><select name="minute"><?for($i=0;$i<60;$i++)echo "<option value='$i'>$i</option>";?></select>:<!-- SECONDS --><select name="second"><?for($i=0;$i<60;$i++)echo "<option value='$i'>$i</option>";?></select><input type="submit" value="submit"></form></body></html>[/code]spec02.php[code]<?PHP$month = $_POST['month'];$day = $_POST['day'];$year = $_POST['year'];$hour = $_POST['hour'];$minute = $_POST['minute'];$second = $_POST['second'];echo "Month = " . $month . "<br>";echo "Day = " . $day . "<br>";echo "Year = " . $year . "<br>";echo "Hour = " . $hour . "<br>";echo "Minute = " . $minute . "<br>";echo "Second = " . $second . "<br>";$artdate = $year . "-" . $month . "-" . $day . "- " . $hour . ":" . $minute . ":" . $second;$timestamp = strtotime($artdate);echo "Unix time stamp = " . $timestamp . "<br>";echo "The date is : " . date('l dS \of F Y h:i:s A', $timestamp);?>[/code]in action - [a href=\"http://www.nstoia.com/spec.php\" target=\"_blank\"]http://www.nstoia.com/spec.php[/a]Hope it is of some help.Lite... Quote Link to comment Share on other sites More sharing options...
yourbrain Posted February 23, 2006 Author Share Posted February 23, 2006 Thanks for the quick replies![b]obsidian[/b] - No, the last three lines are placed at the top of my [b]new_post.php[/b] page, along with other php code. Is that good? Because I tried placing it after my form and at the bottom of the page, but then I get a message telling me [b]article_date[/b] needs to have a value...[b]litebearer[/b] - I saw the example, but how can I make it so that it takes the date from the menu and puts it in the database without taking me more than 30 years back?Oh, and [b]obsidian[/b], nice website! :) Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 23, 2006 Share Posted February 23, 2006 Simply have a field in your database where you store the variable $timestamp (keep in mind that mysql timestamps and Unix timestamps are different - my advice is to just have the field as and integer).when you are ready to display the article and date simply retrieve it and use the[code]echo "The date is : " . date('l dS \of F Y h:i:s A', $timestamp);[/code]to display the dateLite... Quote Link to comment Share on other sites More sharing options...
yourbrain Posted February 24, 2006 Author Share Posted February 24, 2006 I'm starting to feel really stupid. I tried that, but I still get the incorrect date [1970-01-01].Here's the code for my new_article.php page:[code]<?php require_once('Connections/NewsPress.php'); ?><?phpmysql_select_db($database_NewsPress, $NewsPress);$query_rsArticleDates = "SELECT date_format(article_date, '%Y-%c-%e') as ArticleDate FROM np_articles";$rsArticleDates = mysql_query($query_rsArticleDates, $NewsPress) or die(mysql_error());$row_rsArticleDates = mysql_fetch_assoc($rsArticleDates);$totalRows_rsArticleDates = mysql_num_rows($rsArticleDates);?><?phpfunction GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue;}$editFormAction = $_SERVER['PHP_SELF'];if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);}if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO np_articles (article_title, article_category_id, article_description, article_text, article_author, article_date) VALUES (%s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['article_title'], "text"), GetSQLValueString($_POST['article_category_id'], "int"), GetSQLValueString($_POST['article_description'], "text"), GetSQLValueString($_POST['article_text'], "text"), GetSQLValueString($_POST['article_author'], "text"), GetSQLValueString($_POST['article_date'], "date")); mysql_select_db($database_NewsPress, $NewsPress); $Result1 = mysql_query($insertSQL, $NewsPress) or die(mysql_error()); $insertGoTo = "manage_articles.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo));}mysql_select_db($database_NewsPress, $NewsPress);$query_rsMenu = "SELECT * FROM np_categories";$rsMenu = mysql_query($query_rsMenu, $NewsPress) or die(mysql_error());$row_rsMenu = mysql_fetch_assoc($rsMenu);$totalRows_rsMenu = mysql_num_rows($rsMenu);# part of datetime.php include$dbDateTime = mktime($_POST['hour'], $_POST['minute'], $_POST['second'], $_POST['month'], $_POST['day'], $_POST['year']);$dbDateTime = date("Y-m-d H:i:s", $dbDateTime);// Insert $dbDateTime to DATETIME column?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/default.dwt.php" codeOutsideHTMLIsLocked="false" --><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><!-- InstanceBeginEditable name="doctitle" --><title>NewsPress</title><!-- InstanceEndEditable --><!-- InstanceBeginEditable name="head" --><link rel="stylesheet" type="text/css" href="Templates/default.css" /><!-- InstanceEndEditable --></head><body><div id="header"></div><div id="container"><table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="top" id="content"> <div id="spotlight"></div> <!-- InstanceBeginEditable name="content" --> <h1>Post a new Article</h1> <form method="post" name="form1" action="<?php echo $editFormAction; ?>"> <table id="graybox" align="center"> <tr valign="baseline"> <td nowrap align="right">Title:</td> <td><input type="text" name="article_title" value="" size="32"></td> </tr> <tr valign="baseline"> <td nowrap align="right" valign="top">Category:</td> <td><select name="article_category_id" id="article_category_id"> <?phpdo { ?> <option value="<?php echo $row_rsMenu['category_id']?>"><?php echo $row_rsMenu['category_name']?></option> <?php} while ($row_rsMenu = mysql_fetch_assoc($rsMenu)); $rows = mysql_num_rows($rsMenu); if($rows > 0) { mysql_data_seek($rsMenu, 0); $row_rsMenu = mysql_fetch_assoc($rsMenu); }?> </select></td> </tr> <tr valign="baseline"> <td nowrap align="right" valign="top">Description:</td> <td><textarea name="article_description" cols="50" rows="5"></textarea></td> </tr> <tr valign="baseline"> <td nowrap align="right" valign="top">Article:</td> <td><textarea name="article_text" cols="50" rows="5"></textarea></td> </tr> <tr valign="baseline"> <td nowrap align="right">Author:</td> <td><input type="text" name="article_author" value="" size="32"></td> </tr> <tr valign="baseline"> <td nowrap align="right">Date:</td> <td> <?php include('datetime.php'); ?> </td> </tr> <tr valign="baseline"> <td nowrap> </td> <td><hr /><input type="submit" value="Insert record"></td> </tr> </table> <input type="hidden" name="article_date" value="<?php echo $dbDateTime; ?>"> <input type="hidden" name="MM_insert" value="form1"> </form> <!-- InstanceEndEditable --> </td> <td id="nav" valign="top"> <div id="navheader"></div> <div id="navcontent"> <p> <a href="index.php">Front Page</a><br /> <a href="manage_articles.php">Manage Articles</a><br /> <a href="manage_categories.php">Manage Categories</a><br /> <a href="javascript:window.close();">Logout</a> </p> <h1>Archive</h1> <table id="calendar" width="75%" border="0" cellspacing="0" cellpadding="0" align="center"> <tr><td> <?php function build_calendar($month,$year,$day) { /* Declaring the variables */ $daysOfWeek = array('Su','Mo','Tu','We','Th','Fr','Sa'); $firstDayOfMonth = mktime(0,0,0,$month,1,$year); $noDays = date('t',$firstDayOfMonth); $dateComponents = getdate($firstDayOfMonth); $dayOfWeek = $dateComponents['wday']; $monthName = date('F',mktime(0,0,0,$month,1,$year)); global $rsArticleDates; global $_GET; if (mysql_num_rows($rsArticleDates) > 0){ mysql_data_seek($rsArticleDates,0); while($row_rsArticleDates = mysql_fetch_assoc($rsArticleDates)){ $dates[] = $row_rsArticleDates['ArticleDate']; } } /* Computing the previous month. */ if($month == 1) { $mn=12; $yn=$year-1; } else { $mn=$month-1; $yn=$year; } /* Computing the next month. */ if($month == 12) { $mn2=1; $yn2=$year+1; } else { $mn2=$month+1; $yn2=$year; } /* Calendar header: next and previous month links */ $calendar = "<table>"; $calendar .= "<tr><td><a href=day.php?m=$mn&y=$yn&d=$day><</a></td>"; $calendar .="<td colspan=5 align=center>$monthName, $year</td>"; $calendar .="<td><a href=day.php?m=$mn2&y=$yn2&d=$day>></a></td></tr>"; $calendar .="<tr>"; /* Calendar header: Display the days of the week */ foreach($daysOfWeek as $day) { $calendar .= "<td>$day</td>"; } $calendar .= "</tr>"; $calendar .= "<tr>"; $currentDay = 1; /* Fill in the beginning of the calendar body */ if ($dayOfWeek > 0) { $calendar .= "<td colspan='$dayOfWeek'> </td>"; } /* Generate the calendar body */ while ($currentDay <= $noDays) { if ($dayOfWeek == 7) { $dayOfWeek = 0; $calendar .= "</tr><tr>"; } $date = $year."-".$month."-".$currentDay; if (in_array($date,$dates)) { $calendar .= "<td><a href='day.php?m=$month&y=$year&d=$currentDay'>$currentDay</a></td>"; } else { $calendar .= "<td>$currentDay</td>"; } $currentDay++; $dayOfWeek++; } /* Filling in the end of the calendar body */ if ($dayOfWeek != 7) { $remainingDays = 7 - $dayOfWeek; $calendar .= "<td colspan='$remainingDays'> </td>"; } $calendar .= "</table>"; return $calendar; } if (isset($_GET['m']) && isset($_GET['y']) && isset($_GET['d'])){ $month = $_GET['m']; $year = $_GET['y']; $day = $_GET['d']; } else { $dateComponents = getdate(); $month = $dateComponents['mon']; $year = $dateComponents['year']; $day = $dateComponents['mday']; } echo build_calendar($month,$year,$day); ?> </td></tr> </table> <p> </p> </div> <div id="navfooter"></div> </td> </tr></table></div><div id="footer"></div></body><!-- InstanceEnd --></html><?phpmysql_free_result($rsMenu);mysql_free_result($rsArticleDates);?>[/code]And this is the datetime.php page:[code]<!-- DATETIME Drop Menu --><!-- MONTH --> <select name="month" ><? for($i=2;$i<14;$i++){ $month=date("F",mktime(0,0,0,$i,0,0)); echo "<option value='".($i-1)."'>$month</option>"; } ?></select> <!-- DAY --> <select name="day"><? for($i=1;$i<32;$i++) echo "<option value='$i'>$i</option>"; ?></select>, <!-- YEAR --> <select name="year"><? echo date('Y'); $currYear=date("Y"); for($i=$currYear-6;$i<$currYear+5;$i++) echo "<option value='$i'>$i</option>"; ?></select> <!-- HOUR --> <select name="hour"><? for($i=1;$i<25;$i++) echo "<option value='$i'>$i</option>"; ?></select>:<!-- MINUTES --> <select name="minute"><? for($i=0;$i<60;$i++) echo "<option value='$i'>$i</option>"; ?></select>:<!-- SECONDS --> <select name="second"><? for($i=0;$i<60;$i++) echo "<option value='$i'>$i</option>"; ?></select>[/code]Perhaps this will help. From what I understand, the date is being inserted but incorrectly.I'm using Dreamweaver MX to create this, and I use CocoaMySQL to manage the database... Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 24, 2006 Share Posted February 24, 2006 Hmmm, lets try something (we are going to check the validity of your variables)...right after this part pf your code[code]# part of datetime.php include$dbDateTime = mktime($_POST['hour'], $_POST['minute'], $_POST['second'], $_POST['month'], $_POST['day'], $_POST['year']);$dbDateTime = date("Y-m-d H:i:s", $dbDateTime);[/code]enter this snippet[code]echo "the dbDateTime variable is : " . $dbDateTime . "<br>";$month = $_POST['month'];$day = $_POST['day'];$year = $_POST['year'];$hour = $_POST['hour'];$minute = $_POST['minute'];$second = $_POST['second'];echo "Month = " . $month . "<br>";echo "Day = " . $day . "<br>";echo "Year = " . $year . "<br>";echo "Hour = " . $hour . "<br>";echo "Minute = " . $minute . "<br>";echo "Second = " . $second . "<br>";$artdate = $year . "-" . $month . "-" . $day . "- " . $hour . ":" . $minute . ":" . $second;$timestamp = strtotime($artdate);echo "Unix time stamp = " . $timestamp . "<br>";echo "The date is : " . date('l dS \of F Y h:i:s A', $timestamp);exit;[/code]when you then run it, does it return the correct information? Quote Link to comment Share on other sites More sharing options...
yourbrain Posted February 25, 2006 Author Share Posted February 25, 2006 My page comes out blank when I preview it. Quote Link to comment Share on other sites More sharing options...
nshack31 Posted February 25, 2006 Share Posted February 25, 2006 not really a solution, just a question out of interest because im still developing my PHP knowledge, why are you not using....[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$dbDateTime = date("Y-m-d H:i:s", mktime($_POST['hour'], $_POST['minute'], $_POST['second'], $_POST['month'], $_POST['day'], $_POST['year']));[/quote]instead of [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$dbDateTime = mktime($_POST['hour'], $_POST['minute'], $_POST['second'], $_POST['month'], $_POST['day'], $_POST['year']);$dbDateTime = date("Y-m-d H:i:s", $dbDateTime);[/quote]??also what happens if you use H:i:s j-n-Y instead of Y-m-d H:i:s Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 25, 2006 Share Posted February 25, 2006 I noticed that in your code you are getting the date fields out of both the $_POST and $_GET superglobal arrays. Your form specifies "post" as the method. Don't use $_GET, only $_POST. That may be part of your problem.If I were you, I would make a very simple script that just gets the input from your form and processes it, echoing the result to the screen. Once you get that running, build up the rest of the script.Ken Quote Link to comment Share on other sites More sharing options...
yourbrain Posted February 26, 2006 Author Share Posted February 26, 2006 I can't remember where I got the whole $dbDateTime stuff from, I had already built my application and it entered date automatically, but I wanted to have control over the date... so I searched for PHP code, and this one worked well.I'll try $_POST and your other recommendation and let you know how it goes.Thank you for replying. Quote Link to comment Share on other sites More sharing options...
yourbrain Posted March 24, 2006 Author Share Posted March 24, 2006 An update: my limited knowledge of php/mysql has not led me far. I can now enter the date I want as a text only, without the use of a handy drop down menu :([img src=\"http://your.brain.googlepages.com/insert_date.gif\" border=\"0\" alt=\"IPB Image\" /]However, I do not want to give up on this. My computer time is limited now (midterms) but I can spare some time to read any articles or tutorials and try some stuff. Anyway, I might even consider autogenerated code (so if there's any dreamweaver extension out there... lead me to it).Waiting for help. Who wants to type the date, people. Selecting from a drop menu is nicer and safer (codewise). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.