Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Based on our little discussion above, it seems like the easiest thing to do is make your <code>eventdate</code> field an INT data type. That way, when you take the user input as a string ("15/03/2011" for example), you can run that input through the PHP function <code>strtotime()</code> and get a UNIX timestamp from that.</p> <pre><code>&lt;?php $eventts = strtotime($_POST["eventdate"]); $q = "UPDATE eventsDB SET eventdate = ".$eventts." WHERE keyfield = whatever"; $r = mysql_query($q); ?&gt; </code></pre> <p>Note that <code>strtotime()</code> returns an INT (or boolean FALSE) so we're not setting you up for a SQL injection attack above. To query the database, you could then do this:</p> <pre><code>&lt;?php $q = "SELECT * FROM eventsDB WHERE eventdate &gt; UNIX_TIMESTAMP() ORDER BY eventdate ASC LIMIT 30"; $r = mysql_query($q); ?&gt; </code></pre> <p>To answer your question in the comments to this answer:</p> <pre><code>if ( $_POST['eventdate']) $eventdate = mysql_real_escape_string($_POST['eventdate']); </code></pre> <p>Would be replaced with</p> <pre><code>if ( $_POST['eventdate']) $eventdate = strtotime($_POST['eventdate']); </code></pre> <hr> <p>Note that for a production system, I <strong>really</strong> wouldn't recommend putting the admin code in the same page as the display code, and you should use the <code>isset()</code> function to check whether a <code>$_POST</code> array variable has been set (lest you cause all sorts of warnings or notices in your web server logs).</p> <hr> <p>To display the date, you'd use the PHP <code>date()</code> function, specifically:</p> <pre><code>$entry_display = date("d/m/Y", $eventdate); </code></pre> <p>Where <code>$eventdate</code> is the UNIX timestamp you retrieved from the database.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload