man5 Posted September 22, 2015 Share Posted September 22, 2015 Basically what I am trying to do is have an option of choosing an "expiry time" when inserting a record. I will then use that expiry time to make a countdown clock that will expire the record when reached 0; but that's the step after. Right now I am having a bit of issue inserting that expiry time into the database. Here is my code. The error i get in the option value is something like <option value="<b>Notice</b>: Undefined variable...">. <?php $date_added = date('Y-m-d H:i:s'); $expiry_1 = date('Y-m-d H:i:s', strtotime('$date_added + 1 hour')); $expiry_6 = date('Y-m-d H:i:s', strtotime('$date_added + 6 hours')); $expiry_12 = date('Y-m-d H:i:s', strtotime('$date_added + 12 hours')); ?> <fieldset> <label>Expiry Time</label> <select name="expiry_time"> <option value="0">Select Time</option> <option value="<?php echo $expiry_1; ?>">1 hour</option> <option value="<?php echo $expiry_6; ?>">6 hours</option> <option value="<?php echo $expiry_12; ?>">12 hours</option> </select> </fieldset> Quote Link to comment https://forums.phpfreaks.com/topic/298259-adding-expiry-time-when-posting-a-record-can-you-take-a-look-at-this/ Share on other sites More sharing options...
man5 Posted September 22, 2015 Author Share Posted September 22, 2015 (edited) Nevermind. I figured out the problem. Basically the variables($expiry_1..etc) were only called when the form was submitted. So I brought them outside of isset and now I get no errors. But now I see another issue. The hours are not being added to the date. I get the same date for all 3. Can you tell me if my variables are correct? Edited September 22, 2015 by man5 Quote Link to comment https://forums.phpfreaks.com/topic/298259-adding-expiry-time-when-posting-a-record-can-you-take-a-look-at-this/#findComment-1521312 Share on other sites More sharing options...
Solution man5 Posted September 22, 2015 Author Solution Share Posted September 22, 2015 (edited) Alright so I have finally solved my issue. My code in the original post is all correct, except instead of using single quotation( ' ) in the dates variables, I had to use double quotation( " ). Now it shows the dates with added hours. Edited September 22, 2015 by man5 Quote Link to comment https://forums.phpfreaks.com/topic/298259-adding-expiry-time-when-posting-a-record-can-you-take-a-look-at-this/#findComment-1521314 Share on other sites More sharing options...
Barand Posted September 22, 2015 Share Posted September 22, 2015 Alternatively (where $hours is 1, 6 or 12), INSERT INTO mytable (date_added, expire_date) VALUES (NOW(), NOW()+INTERVAL $hours HOUR); Quote Link to comment https://forums.phpfreaks.com/topic/298259-adding-expiry-time-when-posting-a-record-can-you-take-a-look-at-this/#findComment-1521327 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.