开发者

php strtotime issues

Okay, I understand this:

mm/dd/yyyy - 02/01/2003  - strtotime() returns开发者_Go百科 : 1st February 2003
    mm/dd/yy   - 02/01/03    - strtotime() returns : 1st February 2003
    yyyy/mm/dd  - 2003/02/01 - strtotime() returns : 1st February 2003
    dd-mm-yyyy - 01-02-2003  - strtotime() returns : 1st February 2003
    yy-mm-dd   - 03-02-01    - strtotime() returns : 1st February 2003
    yyyy-mm-dd - 2003-02-01  - strtotime() returns : 1st February 2003

So my question is, how do you set up the string on the html page to accept a user's age?

I could just have one box that prompts the user

for month, day and year

or I could split this up

, but is it not true that strtotime will be expecting a string, so does it matter.


If you are trying to do this in strictly PHP, there are ways you can process the dates... For most cases, strtotime() works, but someone might enter a weird format. In this case it might be smart to separate your inputs into 3 textboxes MM / DD / YYYY, then do a $_POST handler and actually generate the time with mktime() (http://us.php.net/mktime).

If I had to choose, I would definitely go the javascript route for user experience. Makes it easier for the user to get the format in two clicks rather than typing out dates and whatnot, and it allows a little less coding for you:

http://www.jqueryui.com/demos/datepicker/

Example:

<script type="text/javascript">
$(function() {
    $("#datepicker").datepicker();
});
</script>



<div class="demo">

<p>Date: <input type="text" id="datepicker"></p>

</div><!-- End demo -->

<div class="demo-description" style="display: none; ">

<p>The datepicker is tied to a standard form input field.  Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay.  Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.</p>

</div><!-- End demo-description -->

PHP will handle the strtotime which will give you the seconds since epoch. You can then store this in your database (int) and then use date('F d, Y', $time); to display your date from seconds.


Look at any entry form on a website where the date/time is important, such as booking tickets... it will:

a) provide the user with the required format "DD/MM/YY" etc

b) have validation with javascript

c) optionally provide a date picker for point and click selection.

this allows you to ensure the format is correct for your serverside interpretation. and perhaps to use stronger functions than strtotime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜