PHP strtotime

WBOY
풀어 주다: 2024-08-29 12:54:57
원래의
1080명이 탐색했습니다.

PHP 프로그래밍 언어에는 날짜 및 날짜 관련 작업을 처리하는 다양한 함수가 있으며, strtotime()이 그 중 하나입니다. PHP 언어에서는 날짜를 다양한 용도로 사용합니다. strtotime() 함수는 PHP 프로그래밍 언어에 내장된 함수입니다. 이 함수는 두 개의 매개변수를 취하는데, 그 중 하나는 필수 매개변수이고 다른 하나는 선택 매개변수입니다. 비즈니스 요구 사항에 따라 날짜를 다루기 위해 문자열을 필수 매개 변수로 전달할 수 있습니다.

광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

구문:

strtotime() 함수를 사용하는 방법은 다양합니다.

strtotime(DateTime, Now);
로그인 후 복사
  • 첫 번째 매개변수(DateTime)는 모두 문자열 형식의 날짜/시간입니다. 이 매개변수는 필수 매개변수이며 이 기능을 사용하는 동안 건너뛸 수 없습니다.
  • 두 번째 매개변수(Now)는 선택사항입니다.
  • PHP는 버전 4+부터 이 기능을 지원합니다. 지금까지 이 기능에는 다양한 확장이 이루어졌습니다.

PHP strtotime은 어떻게 작동하나요?

이 함수는 PHP 언어의 기본 함수 중 하나입니다. 이 기능을 사용하기 전에 추가 플러그인이나 확장이 필요하지 않습니다. 이 함수의 구문을 따르면 비즈니스 요구 사항에 따라 사용할 수 있습니다.

예: 누군가가 현재 날짜(오늘 날짜를 의미)를 알고 싶다면 이 기능을 사용할 수 있습니다. 이를 위해 먼저 현재 타임스탬프를 가져와야 하며 다른 날짜 기능을 사용하여 해당 타임스탬프에서 정확한 날짜를 가져올 수 있습니다.

코드:

<?php
$currentTimeStamp  =strtotime("now");
echo "The output at the time of writing this article was: " .$currentTimeStamp;
?>
로그인 후 복사

이 코드 줄은 현재 타임스탬프를 제공합니다.

출력:

PHP strtotime

이제 다른 함수를 사용하여 이 타임스탬프에서 사람이 읽을 수 있는 날짜를 가져옵니다.

코드:

<?php
$current_date = date('m/d/Y', $currentTimeStamp);
echo "\n Today is: " . $current_date;
?>
로그인 후 복사

출력:

PHP strtotime

PHP strtotime의 예

다음은 언급된 예입니다.

예시 #1

여기서 위의 코드를 프로그램으로 결합하여 출력을 확인합니다.

코드

<?php
$currentTimeStamp  =strtotime("now"); // this line will gives us the current timestamp
echo "The current timestamp is: ". $currentTimeStamp;
$current_date = date('m/d/Y', $currentTimeStamp); // converting the timestamp into the readable format
echo "\n Today is: " . $current_date;
?>
로그인 후 복사

출력:

PHP strtotime

이 출력은 이 프로그램을 작성하고 실행하는 시점에 나온 것입니다. 현재 날짜에 따라 다른 출력을 볼 수 있습니다.

예시 #2

날짜를 타임스탬프로 변경합니다. 우리가 알고 있듯이 문자열 날짜를 타임스탬프로 변경하는 것이 strtotime() 함수의 주요 기능입니다.

코드:

<?php
$date = '27/02/2010';
echo "Actual Date: ". $date."\n";
$date = str_replace('/', '-', $date);
// The nature of the strtotime() function is it takes the string date as a parameter in a specified format only.
// So we are converting the / with the - before passing it to this function we are talking about.
echo "After changing to format of the specified Date: ". $date."\n";
// Now changing the date again to the human-readable form with the different format...
echo "This is Date: ". date('Y-m-d', strtotime($date));
?>
로그인 후 복사

출력:

PHP strtotime

예시 #3

문자열 날짜를 실제 날짜로 변경합니다.

코드:

<?php
echo strtotime("today") . "\n"; // this will give us the timestamp of today
echo strtotime("27 Mar 2020") . "\n";  // this will give us the timestamp of 27 Mar 2020
echo strtotime("+2 hours") . "\n"; // Timestamp of 2 hours later from now
echo strtotime("2 hours") . "\n"; // This will also give the Timestamp of 2 hours later from now
echo strtotime("-2 hours") . "\n"; // This will  give the Timestamp of 2 hours before from now
echo strtotime("3 week") . "\n";   // Timestamp of 3 weeks later from now
echo strtotime("last Monday") . "\n" ;      // This will give the Timestamp of the last Monday
echo strtotime("next Monday");      // This will give the Timestamp of the coming Monday
?>
로그인 후 복사

위 코드를 실행했을 때 출력되는 내용이 다른 것을 볼 수 있습니다. 이 프로그램을 언제 실행하느냐에 따라 전적으로 다릅니다.

출력:

PHP strtotime

예시 #4

위 프로그램을 타임스탬프와 함께 사람이 읽을 수 있는 날짜로 좀 더 수정하세요. 또한 인도 관점에 따라 날짜를 얻을 수 있도록 현재 시간대를 아시아/콜카타로 설정하고 있습니다. 요구 사항에 따라 시간대를 원하는 대로 설정할 수 있습니다.

코드:

<?php
date_default_timezone_set('Asia/Kolkata');
echo strtotime("today") ;
echo " Today is -  " .date('Y-m-d H:i:s', strtotime("today"))."\n";
echo strtotime("27 Mar 2020") ;
echo " Today is -  " .date('Y-m-d H:i:s', strtotime("27 Mar 2020"))."\n";
echo strtotime("+2 hours") ;
echo " After 2 hours from now -  " .date('Y-m-d H:i:s', strtotime("+2 hours"))."\n";
echo strtotime("-2 hours") ;
echo " Before 2 hours from now -   " .date('Y-m-d H:i:s', strtotime("-2 hours"))."\n";
echo strtotime("last Monday") ;
echo " Date and Time of Last Monday " .date('Y-m-d H:i:s', strtotime("last Monday"))."\n";
echo strtotime("next Monday");      // This will give the Timestamp of the coming Monday
echo " Date and Time of coming Monday  " .  date('Y-m-d H:i:s', strtotime("next Monday"))."\n";
?>
로그인 후 복사

출력:

PHP strtotime

결론

필요할 때마다 이 strtotime() PHP 내장 함수를 사용할 수 있습니다. 개발자나 코더는 지정된 문자열 날짜를 매개변수로 알고 있어야 합니다. 문자열 날짜를 Unix 타임스탬프로 변경해야 할 때마다 이 기능을 사용할 수 있습니다.

위 내용은 PHP strtotime의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!