반응형
PHP 날짜 시간이 오늘보다 큼
제 코드에 무슨 문제가 있는지 도와주시겠습니까?항상 오늘 날짜가 2016년이 2015년보다 큰 '01/02/2016'보다 큰 것으로 반환됩니다.
<?php
$date_now = date("m/d/Y");
$date = date_create("01/02/2016");
$date_convert = date_format($date, "m/d/Y");
if ($date_now > $date_convert) {
echo 'greater than';
} else {
echo 'Less than';
}
추신: 2016년 1월 2일은 데이터베이스에서 온 것입니다.
날짜를 비교하고 있지 않습니다.문자열을 비교하고 있습니다.끈 비교의 세계에서,09/17/2015
>01/02/2016
왜냐면09
>01
날짜를 비슷한 문자열 형식으로 입력하거나 비교해야 합니다.DateTime
필적할 만한 물건
<?php
$date_now = date("Y-m-d"); // this format is string comparable
if ($date_now > '2016-01-02') {
echo 'greater than';
}else{
echo 'Less than';
}
또는
<?php
$date_now = new DateTime();
$date2 = new DateTime("01/02/2016");
if ($date_now > $date2) {
echo 'greater than';
}else{
echo 'Less than';
}
날짜를 타임스탬프로 변환하여 비교할 수 있습니다.
<?php
$date_now = time(); //current timestamp
$date_convert = strtotime('2022-08-01');
if ($date_now > $date_convert) {
echo 'greater than';
} else {
echo 'Less than';
}
?>
날짜는 형식이 다를 수 있으므로 비교 전에 타임스탬프로 변환하는 것이 좋습니다.
<?php
$today = date("Y-m-d"); //Today
$date = '2022-06-30'; //Date
if (strtotime($today) > strtotime($date)) {
echo 'Today is greater than date';
}else{
echo 'Today is less than date';
}
언급URL : https://stackoverflow.com/questions/32642417/php-date-time-greater-than-today
반응형
'programing' 카테고리의 다른 글
SwiftUI NavigationLink는 클릭하지 않고 목적지 보기를 즉시 로드합니다. (0) | 2023.08.21 |
---|---|
jQuery로 모든 양식 요소(입력, 텍스트 영역 및 선택)를 가져오려면 어떻게 해야 합니까? (0) | 2023.08.21 |
도커에 볼륨을 추가하지만 하위 폴더 제외 (0) | 2023.08.16 |
.gitignore 특정 파일 제외 (0) | 2023.08.16 |
셀에서 UITableViewCell indexPath를 가져오는 방법은 무엇입니까? (0) | 2023.08.16 |