簡體   English   中英

如果所需條件大於或小於php中的當前日期,如何檢查日期?

[英]How to check the date if the condition required is greater than and less than the current date in php?

我從下面的代碼中需要什么!,我正在獲得輸出“ 完成 ”和“ 待處理 ”的信息。 我應該如何獲得“達到極限”已輸出。

條件需要

  • 如果$current date大於$next_due_on_date ,則應顯示輸出為“ Pending”。

  • 在這種情況下,我需要像“ 30天到期”之類的通知,即在$next_due_on_date達到其限制之前。

  • 如果$current日期小於$next_due_on_date ,則應顯示輸出為“已完成”。

請幫助我解決邏輯。 謝謝!

$current_date = date('Y-m-d G:i:s');   //Current date
$next_due_on_date = '24/10/2018';    //$next_due_on
$next_due_on_date = str_replace('/', '-', $next_due_on_date); //Replacing the format with str_replace       
$highlight_date = date("Y-m-d", strtotime("$next_due_on_date 0 years 0 months -30 days"));    //$highlight_date is to subtract the next_due_on_date with -30 days
if($current_date <= $highlight_date){
    $output = "Completed";
}elseif($current_date > $highlight_date){
    $output = "Pending";
}else{
    $output = "Reached the limit"
}
print_r($output);

@Arun我沒有正確理解您的要求,但據我所知您可以嘗試這樣。

<?php

function s2t($value){
    return strtotime($value);
}

$date1 = 
$date2 = date('Y-m-d',strtotime('+30days'));
$date3 = date('Y-m-d',strtotime('-30days'));

if( s2t($date1) > s2t($date2)  ){
    echo "30+ Days left";
}else{
    echo "30 Days Left";
}

阿倫

我注意到您的代碼中有幾件事。 這里的主要問題是您說您想獲得Reached限制 ,但您的IF不允許這種情況發生。

讓我解釋一下原因:首先比較$current_date <= $highlight_date ,然后比較$current_date > $highlight_date 由於CURRENT_DATE只能是<=或>比highlight_date,最后else將永遠不會運行。

因此,您需要重組IF,以實現此目標。

我不確定我是否很好地理解了您的觀點,但是如果您願意,我想:

  1. 比較$current_date <= ($next_due_on_date - 30 days))
  2. $current_date <= $next_due_on_date
  3. 其他...

因此可能是這樣(我試圖盡可能少地更改您的代碼):

$current_date = date('Y-m-d G:i:s');   //Current date
$next_due_on_date = '24/10/2018';    //$next_due_on
$next_due_on_date = str_replace('/', '-', $next_due_on_date); //Replacing the format with str_replace       
$highlight_date = date("Y-m-d", strtotime("$next_due_on_date 0 years 0 months -30 days"));    //$highlight_date is to subtract the next_due_on_date with -30 days
if($current_date <= $highlight_date){
    $output = "Completed";
}elseif($current_date <= $next_due_on_date){
    $output = "Pending";
}else{
    $output = "Reached the limit";
}
print_r($output);

是那個嗎?

下面的代碼運行良好。 請檢查,它可能用於您要比較兩個日期的任何概念。

$current_date = date('Y-m-d G:i:s');
$next_due_on_date = '27/09/2018';
$next_due_on_date = str_replace('/', '-', $next_due_on_date);        
$highlight_lesser_thirdty_date = date("Y-m-d", strtotime("$next_due_on_date 0 years 0 months -30 days"));
$highlight_lesser_thirdtyone_date = date("Y-m-d", strtotime("$next_due_on_date 0 years 1 months -31 days"));
if(($current_date <= $highlight_lesser_thirdtyone_date ) && ($current_date <= $highlight_lesser_thirdty_date)) {
     $status = "Completed";
}elseif(($current_date < $highlight_lesser_thirdtyone_date) && ($current_date > $highlight_lesser_thirdty_date)){
     $status = "Calibration to be conducted";
}elseif($current_date < $next_due_on_date){
     $status = "Pending";
}
print_r($calibration_status);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM