网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站建设 > 代码交流
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

代码交流 ASP,PHP,JSP等网站源代码下载与交流。

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   IP: 222.76.215.18
Old 2008-01-24, 10:24 PM
car car is offline
高级会员
 
Join Date: 2006-05-14
Posts: 534
car 正向着好的方向发展
Default php日期验证

日期
PHP具有非常出色的日期工具,极大地简化了日期标准化和检验工作。特别是strtotime()函数能够把绝大多数表示日期的字符串转化为UNIX时标,如果转化失败,它会返回False。这个工具拥有非常强大的功能,几乎不需要我们再做其他工作。在转化为时标之后,我们就可以根据需要使用date()将它格式化为任何式样,如程序清单11.6.1所示。

程序清单11.6.1 使用strtotime()和date()实现日期标准化和检验

<?php

// Run against various dates

$dates = array('Dec 3 1973', '12/3/73', '1973-12-3',

'1/0/2005', '3/32/2004', '0/1/0', '12/3/973', '2/31/2006', '13/30/05');

// Initialise the timezone settings, and loop over all the values:

date_default_timezone_set('America/New_York');

foreach ($dates as $d) {

// Convert it & validate at the same time:

$output = strtotime($d);

// Prepare the display, either 'BAD DATE' or a formatted version

$disp = ($output === false) ? 'BAD DATE' : date('m/d/Y', $output);

echo "<p>{$d} = {$disp}</p>\n";

}

</?>

需要提醒的是,strtotime()函数的某些功能在一开始可能会让人感到迷惑。例如它允许任何一个月的日期都可以到31日,但如果相应的月份不应该有31天,它会自动变化到有效的日期,因此11/31/2005会转化为12/1/2005。它还允许第0天(实际上返回的日期是上一个月的最后一天),而0月表示十二月,0年表示2000年。
Reply With Quote
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 11:08 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.