2009-12-01 50 views
0

好吧,我明白這一點:PHP的strtotime發出

mm/dd/yyyy - 02/01/2003 - strtotime() returns : 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 

所以我的問題是,你如何設置的HTML頁面上的字符串來接受用戶的年齡?

我可以只是提示用戶

月份,日期和年份一個箱

我也可以拆分這件事

,但它是不正確的的strtotime將期待字符串,所以它是重要的。

回答

2

如果您試圖嚴格按照PHP執行此操作,則可以使用多種方法處理日期......對於大多數情況,strtotime()可以工作,但有人可能會輸入奇怪的格式。在這種情況下,將輸入分隔爲3個文本框MM/DD/YYYY可能很明智,然後執行$ _POST處理程序並實際使用mktime()(http://us.php.net/mktime)生成時間。

如果我不得不選擇,我肯定會去用戶體驗的JavaScript路線。使得用戶更容易獲得在兩次點擊的格式,而不是打字了日期和諸如此類的東西,它可以讓你少一點編碼:

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

例子:

<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將處理時間,它會給你從時代開始的秒數。然後,您可以將其存儲在您的數據庫(int)中,然後使用date('F d,Y',$ time);從幾秒鐘顯示你的日期。

2

看網站上的任何條目形式,其中的日期/時間是很重要的,如訂票...這將:

一)提供所需格式的用戶「DD/MM/YY」等

b)中具有與JavaScript

ç驗證)任選提供點日期選擇器和選擇點擊。

這使您可以確保格式對您的服務器端解釋是正確的。並可能使用比時間更強的功能。

+0

謝謝你完全忽略了,但是,這只是爲了學習的目的,我只是學習PHP,我甚至沒有與js搞砸。 – Newb 2009-12-01 15:11:11