HOWTO · PHP

PHP에서 DatePicker로 작업하기

이 튜토리얼에서는 내장 함수와 타사 PHP 라이브러리를 사용하여 PHP에서 DatePicker로 작업하는 방법에 대해 설명합니다.

이 페이지의 내용

사이트 내에서 양식을 작성할 때 DatePicker를 사용하여 날짜를 선택해야 할 수도 있습니다. 예를 들어, 생년월일 또는 과정 완료 날짜.

그렇게 하려면 HTML과 JavaScript를 사용할 수 있습니다. 그러나 날짜를 사용하려면 사용자가 서버 측에서 선택한 날짜를 비교해야 합니다.

클라이언트 측에서는 PHP를 사용할 수 없지만 서버 측에서는 DatePicker 값을 비교할 수 있습니다. 이 기사에서는 두 가지 함수를 사용하여 클라이언트 측의 DatePicker와 DatePickers용 PHP 라이브러리에서 얻은 값을 비교합니다.

strtotime() 및 DateTime()을 사용하여 PHP 내에서 날짜 생성

JS DatePicker를 사용하면 사용자가 선택한 날짜의 값이 문자열에 저장될 가능성이 큽니다. 우리는 좋은 사용법을 허용하기 위해 strtotime() 함수를 사용할 것입니다.

이 예에서 사용자의 시간을 기준 날짜와 비교하여 사용자가 18세 이상인지 알 수 있습니다.

암호:

<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <?php

    function above_18($arg) {
      $baseline_date = strtotime("2004-03-26");
      $users_date = strtotime($arg);

      if ($user_date < $baseline_date) {
          return "You are below the age of 18.";
      } else {
        return "You are above the age of 18. Continue with your registration process";
      }
    }

    // Obtain the date of birth of user
    // and place as the argument for the above_18() function
    $notification = above_18("2007-10-29");
    print_r($notification);

    ?>
  </body>
</html>

출력:

You are below the age of 18.

이제 DateTime() 메서드를 사용하여 DatePicker 값이 서버 측의 값과 다른 동일한 결과를 얻습니다.

암호:

<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <?php

    function above_18($arg) {
      $baseline_date = new DateTime("2004-03-26");
      $users_date = new DateTime($arg);

      if ($user_date < $baseline_date) {
          return "You are below the age of 18.";
      } else {
        return "You are above the age of 18. Continue with your registration process";
      }
    }

    // Obtain the date of birth of user
    // and place as the argument for the above_18() function
    $notification = above_18("07-10-29");
    print_r($notification);
    ?>
  </body>
</html>

출력:

You are below the age of 18.

DatePicker 기능에 PHP용 Essential JS 사용

JS DatePicker 값을 비교하는 것 외에도 Essential JS for PHP라는 타사 라이브러리를 사용하여 PHP DatePicker를 만들 수 있습니다.

다음 예제에서는 EJ 네임스페이스에서 PHP 래퍼 클래스를 호출하여 PHP DatePicker 컨트롤을 만들고 minDate 및 maxDate 메서드를 통해 최소 및 최대 날짜를 설정합니다.

암호:

<?php
        $date = new EJ\DatePicker("datePicker");
        echo $date->value(new DateTime())->
                minDate(new DateTime("11/1/2016"))->
                maxDate(new DateTime("11/24/2016"))->
                render();
?>

출력:

PHP 라이브러리용 Essential JS를 사용하는 Datepicker