简体   繁体   中英

How can I echo a date so many days from now using PHP?

I am making a script that will allow admins to ban IPs. I am currently implementing the ban length system, however I am trying to echo the lift date next to each select option.

Here is my HTML for the dropdown select:

             <strong>Select Ban Length</strong>
       <div class="select-style">
  <select>
    <optgroup label="Temporary Ban Options">
    <option value="1">1 Day</option>
    <option value="3">3 Days</option>
    <option value="7">7 Days</option>
    <option value="14">14 Days</option>
    </optgroup>
    <optgroup label="Permanent Ban Options">
      <option value="PERMANENT">Permanent - Never Lift Ban</option>
    </optgroup>
  </select>
</div>

What I am asking is how can I echo the lift date next to each item? For example:

在此处输入图片说明

I have started with <?php echo date('Ymd H:i:s'); ?> <?php echo date('Ymd H:i:s'); ?> to get the current date but how can I, as shown in the image above achieve something similar where the lift date is beside the ban length?

You would use strtotime 's automatic + n days ability using this date string mdY H:i A to give what you asked for in your example image.

<strong>Select Ban Length</strong>
<div class="select-style">
  <select>
    <optgroup label="Temporary Ban Options">
    <?php
    foreach (array(1, 3, 7, 14) as $d){
        // setting $date and $text separately just for readability here
        $text = $d . ' Day' . ($d===1? '': 's');
        $date = date('m-d-Y H:i A', strtotime('+ ' . $d .' days'));
        echo '<option value="'.$d.'">' . $text . ' (' . $date . ')</option>';
    }
    ?>
    </optgroup>
    <optgroup label="Permanent Ban Options">
      <option value="PERMANENT">Permanent - Never Lift Ban</option>
    </optgroup>
  </select>
</div>

Here is how you'd do it:

  <select>
    <optgroup label="Temporary Ban Options">
    <option value="1">1 Day (<?= date('Y-m-d H:i:s', strtotime("+1 day")) ?>)</option>
    <option value="3">3 Days (<?= date('Y-m-d H:i:s', strtotime("+3 days")) ?>)</option>
    <option value="7">7 Days (<?= date('Y-m-d H:i:s', strtotime("+7 days")) ?>)</option>
    <option value="14">14 Days (<?= date('Y-m-d H:i:s', strtotime("+14 days")) ?>)</option>
    </optgroup>
    <optgroup label="Permanent Ban Options">
      <option value="PERMANENT">Permanent - Never Lift Ban</option>
    </optgroup>
  </select>

Use similar code:

 echo date('Y-m-d H:i:s', strtotime("+1 day"))

In your case:

<option value="1">1 Day (<?= date('Y-m-d H:i:s', strtotime("+1 day")) ?>)</option>
    <option value="3">3 Days (<?= date('Y-m-d H:i:s', strtotime("+3 days")) ?>)</option>
    <option value="7">7 Days (<?= date('Y-m-d H:i:s', strtotime("+7 days")) ?>)</option>
    <option value="14">14 Days (<?= date('Y-m-d H:i:s', strtotime("+14 days")) ?>)</option>

Read more about strtotime function: http://www.w3schools.com/php/func_date_strtotime.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM