×

Please Login or Register to continue.

2
(179 Views)

I am working in PHP programming. I want to create a expire condition in PHP. I have tried but did not get any solution. I want to show expired and active using two dates in PHP .  I have a date in my database and I will use that date to make expire condition in PHP and MSYQL. How can i combine to dates and get expired active date using PHP?

i'm trying like this :-

<?php
$today_date = date("d M, Y");
$last_date = "20 Mar, 2022";
if($last_date < $today_date){
 echo "expired";
}else{
 echo "active";
 }
?>

How can I create an expired and active condition on behalf of two dates like - the current date and database date? 

(140 Points)
in PHP

Share

2 Answers
(3.1K Points)
(edited)
2

If you want to create two conditions when active and or when expired based on two dates - Current date and your database date. You can use less than(<) operator (dbdate<current date) to create an expired condition in PHP. It will be helping you combine two dates for expired date. For example, if you want to display a deal expired or active on your website, you can use this condition to display whether your deal has expired or is active. Here you will learn how to compare two dates and find the expired date and active date. In this case, you will need the correct form of two dates. You can check the date and time formats here. Date and time functions in PHP with formats

 <?php
$today_date = date("Y-m-d");
$last_date = "2022-03-16";
if($last_date <$today_date){
echo "Expired";
 }else{
echo "Active";
 }
?>

Change the last date and check whether you will get an expired message or an active message.


Comment

(9.2K Points)
(edited)
1

The date expired and the active date depends on the current date. You should combine two dates to get the date expired and active in PHP. Create an expired and active condition using an if-else statement in PHP. You have to make a condition like - 

if(end_date<current_date) 
{
echo 'Date expired';
}
else 
{
echo 'Date active';
}

In your case, you are missing the correct form of dates.

$today_date = date("Y-m-d");
 $last_date = "2022-03-20";

Now, you will get expired when the last_date will be less than the current date and will get active when the last_date will be greater than the current date. 


Comment

Related Questions :-

Your Answer

You can post your answer after login..