首頁  >  文章  >  後端開發  >  php中strtotime函數用法實例詳解

php中strtotime函數用法實例詳解

怪我咯
怪我咯原創
2017-07-06 10:23:031213瀏覽

這篇文章主要介紹了php中strtotime函數的用法,以實例形式詳細分析了strtotime函數的參數含義與相應的用法,適用於時間格式變換的情況,需要的朋友可以參考下

本文實例講述了php中strtotime函數用法。分享給大家供大家參考。具體如下:

strtotime(字串$時間[,詮釋$現在])int strtotime(string $time [,int $now] 此函數期望得到一個包含美國英語日期格式,並會嘗試解析成一個Unix時間戳(多少秒自1970年1月1日00:00:00星期一該格式),相對於現在提供的時間戳,或當前時間如果現在不提供

這個函數將使用TZ環境變數(如果有)來計算時間戳記,自PHP 5.1.0有更容易的方法來確定所使用的所有/日期時間函數的時區,這一過程是解釋在date_default_timezone_get()函數的一頁.

解析的字串,在PHP 5.0.0,不允許在微秒的時間,自PHP 5.0.0他們是允許的,但忽略.

現在哪些是作為計算基數相對日期使用時間戳.

返回值:在成功返回一個時間戳,否則返回FALSE,前到PHP 5.1.0,這個函數將回傳失敗-1.

現在我們來看看strtotime字元轉換成時間的函數實例,程式碼如下:

程式碼如下:

<?php 
//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(&#39;D d M Y&#39;, strtotime(&#39;+&#39;.$i.&#39; week&#39;,$date_begin)); 
} 
return $nextweek; 
} 
/// end function  
/// example of a select date  
// var 
$date_begin = strtotime(&#39;06-05-2010&#39;); //D Day Month Year  - like function format. 
$nbrweek=52; 
// call function 
$result=nextWeeksDay($date_begin,$nbrweek); 
// Preview  
for($i = 1; $i <= $nbrweek; $i++)  { 
echo &#39;<br> - &#39;.$result[$i]; 
} 
?>
<?php 
$str = &#39;Not Good&#39;; 
 
// 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(&#39;l dS o F Y h:i:s A&#39;, $timestamp); 
} 
?>  
<?php 
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"), " "; 
?>

這是一個快速函數計算在一年期間,“工作天”,“工作日”是指那些沒有周末,沒有假期在$數組中指定的假日,實例代碼如下:

代碼如下:

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"));

以上是php中strtotime函數用法實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn