简体   繁体   中英

Problem making timestamp out of date received from a dropdown select menu.

I want to only allow visitors to choose from certain dates, for example the next 20 days. So I have a form which looks like this.

<select name="data">
<?php
    $day = date('d');
    $i = 1;
    while($i < 20) {
        $i++;
        $data = (date("d-M-Y",mktime(0,0,0,7,$day,2011)) . "<br />");
        echo "<option value='".$data."'>".$data."</option>";
        $day++;
    }
?>
</select>

In the controller (I use Code Igniter), I can echo the date but I cannot make a timestamp out of the date.

$data = $this->input->post('data');
$timestamp = strtotime('$data');
echo $timestamp;

The echo $timestamp doesn't work. It displays nothing.

My $date is 23-08-2011 However, echo strtotime('23-08-2011'); works.

I even tried it outside of Code Igniter (1 form, 1 processformfile) and it still doesn't work.

Do you have any ideas why it doesn't work?

Thanks a lot.

Well, $timestamp=strtotime('$data'); wouldn't work because you're treating $data as a literal string. Remove the quotes:

$timestamp=strtotime($data);

Also, you're setting $data as a formatted string with a line break:

$data=(date("d-M-Y",mktime(0,0,0,7,$day,2011))."<br />");

Try:

$data=date("d-M-Y",mktime(0,0,0,7,$day,2011);

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