×

Please Login or Register to continue.

2
(3.3K 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.4K 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.4K 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
However, if the MySql database column is not set it will default to 000-00-00. So comparing today to the DB field will always return "Date expired", problem.
rasberry05 09 Mar 2023
Is there any particular reason why the default is 0000-00-00? If you allow the column to have NULL values in the table, you won't have to use PHP to look for the special zero date. if($date == '0000-00-00') { //set your code lines if the default date is 0000-00-00 } else { // same code lines }
shakti 13 Mar 2023

Related Questions :-

Featured Items:-


Certificate system in PHP website | PHP scripts

$22



Home Credito loan finance management multipurpose system in PHP

$65



Result management system with Marksheet in PHP website | PHP Scripts

$39




Your Answer

You can post your answer after login..