>  기사  >  백엔드 개발  >  PHP의 strtotime 함수 사용 예에 ​​대한 자세한 설명

PHP의 strtotime 함수 사용 예에 ​​대한 자세한 설명

怪我咯
怪我咯원래의
2017-07-06 10:23:031215검색

이 글은 주로 PHP에서 strtotime 함수의 사용법을 소개하며, strtotime 함수의 매개변수 및 해당 사용법을 예제 형식으로 자세히 설명합니다. 시간 형식 변환이 필요한 친구들이 참고할 수 있습니다. to it

이 글의 예제는 PHP에서 strtotime 함수의 사용법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

strtotime(string$time[, int $now])int strtotime(string $time [,int $now] 이 함수는 미국 영어가 포함된 날짜 형식을 예상하고 이를 구문 분석하려고 시도합니다. Unix 타임스탬프 (이 형식에서는 1970년 1월 1일 월요일 00:00:00 이후 몇 초인지), 현재 제공된 타임스탬프 또는 지금 제공되지 않은 경우 현재 시간을 기준으로

이 함수는 타임스탬프를 계산하기 위한 TZ 환경 변수(있는 경우), PHP 5.1.0부터 all/datetime 함수를 사용하여 시간대를 결정하는 더 쉬운 방법이 있습니다. 이 프로세스는 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"), " "; 
?>

1년 동안의 "근무일"을 계산하는 빠른 함수입니다. "근무일"은 $array에 주말이나 공휴일이 지정되지 않은 휴일을 의미합니다.

코드는 다음과 같습니다:

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으로 문의하세요.