十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂(yōu)售后,網(wǎng)站問(wèn)題一站解決
本篇文章為大家展示了strtotime函數(shù)如何在php中使用,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
strtotime(字符串$時(shí)間[,詮釋$現(xiàn)在])int strtotime(string $time [,int $now] 該函數(shù)期望得到一個(gè)包含美國(guó)英語(yǔ)日期格式,并會(huì)嘗試解析成一個(gè)Unix時(shí)間戳(多少秒自1970年1月1日00:00:00星期一該格式),相對(duì)于現(xiàn)在提供的時(shí)間戳,或當(dāng)前時(shí)間如果現(xiàn)在不提供
這個(gè)函數(shù)將使用TZ環(huán)境變量(如果有)來(lái)計(jì)算時(shí)間戳,自PHP 5.1.0有更容易的方法來(lái)確定所使用的所有/日期時(shí)間函數(shù)的時(shí)區(qū),這一過(guò)程是解釋在date_default_timezone_get()函數(shù)的一頁(yè).
解析的字符串,在PHP 5.0.0,不允許在微秒的時(shí)間,自PHP 5.0.0他們是允許的,但忽略.
現(xiàn)在哪些是作為計(jì)算基數(shù)相對(duì)日期使用時(shí)間戳.
返回值:在成功返回一個(gè)時(shí)間戳,否則返回FALSE,前到PHP 5.1.0,這個(gè)函數(shù)將返回失敗-1.
現(xiàn)在我們來(lái)看看strtotime字符轉(zhuǎn)換成時(shí)間的函數(shù)實(shí)例,代碼如下:
復(fù)制代碼 代碼如下:
//function
function nextWeeksDay($date_begin,$nbrweek)
{
$nextweek=array();
for($i = 1; $i <= $nbrweek; $i++) { // 52 week in one year of coursewww.phpfensi.com
$nextweek[$i]=date('D d M Y', strtotime('+'.$i.' week',$date_begin));
}
return $nextweek;
}
/// end function
/// example of a select date
// var
$date_begin = strtotime('06-05-2010'); //D Day Month Year - like function format.
$nbrweek=52;
// call function
$result=nextWeeksDay($date_begin,$nbrweek);
// Preview
for($i = 1; $i <= $nbrweek; $i++) {
echo '
- '.$result[$i];
}
?>
$str = 'Not Good';
// previous to PHP 5.1.0 you would compare with -1, instead of false
if (($timestamp = strtotime($str)) === false) {
echo "The string ($str) is bogus";
} else {
echo "$str == " . date('l dS o F Y h:i:s A', $timestamp);
}
?>
echo strtotime("now"), " ";
echo strtotime("10 September 2000"), " ";
echo strtotime("+1 day"), " ";
echo strtotime("+1 week"), " ";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), " ";
echo strtotime("next Thursday"), " ";
echo strtotime("last Monday"), " ";
?>
這是一個(gè)快速函數(shù)計(jì)算在一年期間,“工作天”,“工作日”是指那些沒(méi)有周末,沒(méi)有假期在$數(shù)組中指定的假日,實(shí)例代碼如下:
復(fù)制代碼 代碼如下:
function get_working_days($to_date) {
$holidays = array(
1 => array(10), //2011 ...
2 => array(11),
3 => array(21), //... 2011
4 => array(29,30), //2010 ...
5 => array(3,4,5),
6 => array(),
7 => array(19),
8 => array(11,12,13),
9 => array(20,23),
10 => array(11),
11 => array(3,23),
12 => array(23) //... 2010
);
for($to_date, $w = 0, $i = 0, $x = time(); $x < $to_date; $i++, $x = strtotime("+$i day")) {
if(date("N",$x) < 6 &! in_array(date("j",$x),$holidays[date("n",$x)])) $w++;
}
return $w;
}
//Usage:
echo get_working_days(strtotime("2011-01-08"));
上述內(nèi)容就是strtotime函數(shù)如何在php中使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。