March 28, 20188 yr I'm trying to implement an anti-bot timer on my contact form as suggested by @BrowserBugs here But I'd also like to include the time in the email so I can check what I'm doing and because it would be useful. If I simply put the posted value 'sent' into a variable and include that in the email I get the complete php code from the form input not the date-time. And if I use strtotime on 'sent' first I don't get anything in the resulting variable. What am I missing? TIA Paul
March 28, 20188 yr Ahoy. If you could post the form i can take a look. If you just want to know the date and time the email was sent use; $date_sent = date('Y-m-d H:i:s'); this gets the current server date and time at the point of process. If you want a different format look at date format.
March 29, 20188 yr Author Thanks, my php is not great so apologies if this is obvious. What I was specifically trying to do was get the value of 'sent' from your <input name="sent" type="hidden" value="<?php echo date('Y-m-d H:i:s');?>"> but when I do $submitted = $_POST['sent']; the variable $submitted contains <?php echo date('Y-m-d H:i:s');?> not the date string I was expecting. Which makes me think I'm missing something. I tried combinations of strtotime and dateformat but can't get it to work and although my php skills may be letting me down I wondered again if I'm missing something more fundamental.
March 29, 20188 yr Ah! The $submitted is already a date time value, e.g. 2018-03-29 10:28:16. To change it's format to 23/03/2018 @ 10:28 would be; <?php echo date("d/m/Y @ H:i", strtotime($submitted)); ?> What strtotime does is "parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT)" (source). With date the first bit is the output format, the second value is the time which if left out defaults to now, but if you give it "2018-03-29 10:28:16" it turns that into the unix timestamp then turns it back into the format you wanted.
March 29, 20188 yr Author Yes, I was expecting $submitted to be a date but currently it contains the whole php code verbatim from the input section of the form and not 2018-03-29 10:28:16 as I was expecting. Thanks for clarification on strtotime - that explains why my time check doesn't work because it's not getting a date as input but the string intended to generate the date. Do I need to put $submitted = $_POST['sent']; into a if($_SERVER['REQUEST_METHOD']=='POST') { $submitted=$_POST('sent'); } construct?
March 29, 20188 yr 1 hour ago, DonkeyWorx said: Yes, I was expecting $submitted to be a date but currently it contains the whole php code verbatim from the input section of the form and not 2018-03-29 10:28:16 as I was expecting. Sounds like something is processing all fields, what else is the code doing? A quick test is a clean sheet, try; <?php $msg = ""; if($_SERVER['REQUEST_METHOD']=="POST") { $msg .= "I have posted"; $date_sent = trim($_POST['sent']); if($date_sent!="") { $msg .= " at " . $date_sent . " also known as " . date("jS F, Y", strtotime($date_sent)) . " or " . date("d/m/Y @ H:i", strtotime($date_sent)); } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Test Post</title> </head> <body> <?php if($msg!="") { echo "<p>" . $msg . "</p>"; } ?> <form method="post"> <input type="hidden" name="sent" value="<?php echo date('Y-m-d H:i:s'); ?>"> <input name="submit" type="submit" value="1"> </form> </body> </html>
March 29, 20188 yr Author Many thanks - that successfully highlighted my stupidity. The form was in an html page not a php file so the php wasn't executing. Knew I was missing something obvious.
March 29, 20188 yr 1 hour ago, DonkeyWorx said: Knew I was missing something obvious. All guilty there mate
Create an account or sign in to comment