PHP에서 두 날짜를 어떻게 비교할 수 있습니까?
PHP에서 두 날짜를 어떻게 비교할 수 있습니까?
데이터베이스에서 날짜는 2011-10-2와 같습니다.
오늘 날짜와 데이터베이스 날짜를 비교하여 더 큰 날짜를 확인하려면 어떻게해야합니까?
나는 이것을 시도했다.
$today = date("Y-m-d");
$expire = $row->expireDate //from db
if($today < $expireDate) { //do something; }
그러나 실제로는 그렇게 작동하지 않습니다. 또 다른 방법은 무엇입니까?
업데이트 : 나는이 게시물이 다소 오래되었다는 것을 알고 있지만, 단지 탄소를 언급하고 싶었습니다. 확인해 보세요 : 카본
데이터베이스에서 날짜는 2011-10-2와 같습니다.
YYYY-MM-DD에 저장하면 '1'> '0'등으로 인해 문자열 비교가 작동합니다.
모든 날짜가 1970 년 1 월 1 일 이후 인 경우 다음과 같이 사용할 수 있습니다.
$today = date("Y-m-d");
$expire = $row->expireDate; //from database
$today_time = strtotime($today);
$expire_time = strtotime($expire);
if ($expire_time < $today_time) { /* do Something */ }
PHP 5> = 5.2.0을 사용하는 경우 DateTime 클래스를 사용할 수 있습니다.
$today_dt = new DateTime($today);
$expire_dt = new DateTime($expire);
if ($expire_dt < $today_dt) { /* Do something */ }
아니면이 라인을 따라 뭔가.
이미 주어진 답변을 칭찬하려면 다음 예를 참조하십시오.
$today = new DateTime('');
$expireDate = new DateTime($row->expireDate); //from database
if($today->format("Y-m-d") < $expireDate->format("Y-m-d")) {
//do something;
}
업데이트 : 또는 간단한 old- date () 함수를 사용하십시오.
if(date('Y-m-d') < date('Y-m-d', strtotime($expire_date))){
//echo not yet expired!
}
나는 PHP로 이것을하지 않을 것입니다. 데이터베이스는 오늘날의 요일을 알아야합니다 (예를 들어 MySQL-> NOW () 사용). 따라서 쿼리 내에서 비교하고 사용 된 날짜 유형에 따라 아무런 문제없이 결과를 반환하는 것이 매우 쉽습니다.
SELECT IF(expireDate < NOW(),TRUE,FALSE) as isExpired FROM tableName
$today = date('Y-m-d');//Y-m-d H:i:s
$expireDate = new DateTime($row->expireDate);// From db
$date1=date_create($today);
$date2=date_create($expireDate->format('Y-m-d'));
$diff=date_diff($date1,$date2);
//echo $timeDiff;
if($diff->days >= 30){
echo "Expired.";
}else{
echo "Not expired.";
}
$today_date=date("Y-m-d");
$entered_date=$_POST['date'];
$dateTimestamp1 = strtotime($today_date);
$dateTimestamp2 = strtotime($entered_date);
$diff= $dateTimestamp1-$dateTimestamp2;
//echo $diff;
if ($diff<=0)
{
echo "Enter a valid date";
}
두 날짜 의 차이 를 분 단위 로 얻는 방법은 다음과 같습니다 .
// set dates
$date_compare1= date("d-m-Y h:i:s a", strtotime($date1));
// date now
$date_compare2= date("d-m-Y h:i:s a", strtotime($date2));
// calculate the difference
$difference = strtotime($date_compare1) - strtotime($date_compare2);
$difference_in_minutes = $difference / 60;
echo $difference_in_minutes;
간단한 PHP를 사용하여 날짜를 비교할 수 있습니다.
$date = new simpleDate();
echo $date->now()->compare($expire_date)->isBeforeOrEqual();
이것은 당신에게 참 또는 거짓을 줄 것입니다.
더 많은 예제를 위해 튜토리얼을 확인할 수 있습니다. 하시기 바랍니다 여기를 클릭 .
나는 그 문제도 있었고 다음과 같이 해결했다.
$today = date("Ymd");
$expire = str_replace('-', '', $row->expireDate); //from db
if(($today - $expire) > $NUMBER_OF_DAYS)
{
//do something;
}
간단히 확인할 수 있습니다 ..
if ($startdate < $date) {// do something}
if ($startdate > $date) {// do something}
하지만 두 날짜는 같은 형식이어야합니다
Ymd 또는 dmY
기타
first of all, try to give the format you want to the current date time of your server:
Obtain current date time
$current_date = getdate();
Separate date and time to manage them as you wish:
$current_date_only = $current_date[year].'-'.$current_date[mon].'-'.$current_date[mday]; $current_time_only = $current_date['hours'].':'.$current_date['minutes'].':'.$current_date['seconds'];
Compare it depending if you are using donly date or datetime in your DB:
$today = $current_date_only.' '.$current_time_only;
or
$today = $current_date_only;
if($today < $expireDate)
hope it helps
Here's my spin on how to get the difference in days between two dates with PHP. Note the use of '!' in the format to discard the time part of the dates, thanks to info from DateTime createFromFormat without time.
$today = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
$wanted = DateTime::createFromFormat('!d-m-Y', $row["WANTED_DELIVERY_DATE"]);
$diff = $today->diff($wanted);
$days = $diff->days;
if (($diff->invert) != 0) $days = -1 * $days;
$overdue = (($days < 0) ? true : false);
print "<!-- (".(($days > 0) ? '+' : '').($days).") -->\n";
Found the answer on a blog and it's as simple as:
strtotime(date("Y"."-01-01")) -strtotime($newdate))/86400
And you'll get the days between the 2 dates.
참고URL : https://stackoverflow.com/questions/3847736/how-can-i-compare-two-dates-in-php
'Programming' 카테고리의 다른 글
| Visual Studio 프로젝트의 폴더에 대한 "링크로 추가" (0) | 2020.07.13 |
|---|---|
| uiwebview에서 로컬 HTML을 사용하여 상대 경로에서 리소스로드 (0) | 2020.07.13 |
| 파이썬 인터프리터 쉘에서 마지막 명령을 반복하는 방법은 무엇입니까? (0) | 2020.07.13 |
| 검색 창 위젯의 배경 드로어 블 변경 (0) | 2020.07.13 |
| React Native에서 뷰 크기 가져 오기 (0) | 2020.07.13 |