How can I compare two dates in PHP

0 votes

The date is stored in the database in the following format

2020-08-27

If I wanted to compare today's date against the date in the database to see which one is greater, how would I do it?

I tried this:

$today = date("Y-m-d");
$expire = $row->expireDate //from db

if($today < $expireDate) { //do something; }

but it doesn't really work that way. What's another way of doing it?How can I compare two dates in PHP?

Aug 27, 2020 in PHP by kartik
• 37,510 points
1,130 views

1 answer to this question.

0 votes

Hello @kartik,

If all your dates are posterior to the 1st of January of 1970, you could use something like:

$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 */ }

If you are using PHP 5 >= 5.2.0, you could use the DateTime class:

$today_dt = new DateTime($today);
$expire_dt = new DateTime($expire);

if ($expire_dt < $today_dt) { /* Do something */ }

Or something along these lines.

Hope it helps!!
Thank you!!

answered Aug 27, 2020 by Niroj
• 82,880 points

Related Questions In PHP

0 votes
1 answer

How can I upload Multiple file in php?

Hello @kartik, Multiple files can be selected and ...READ MORE

answered Aug 27, 2020 in PHP by Niroj
• 82,880 points
588 views
0 votes
1 answer

How can I get useful error messages in PHP?

Hello @kartik, The following enables all errors: ini_set('display_startup_errors', 1); ini_set('display_errors', ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,880 points
322 views
0 votes
1 answer

How can I declare a global variable in php?

Hello @kartik, The $GLOBALS array can be used instead: $GLOBALS['a'] = ...READ MORE

answered Oct 1, 2020 in PHP by Niroj
• 82,880 points
518 views
0 votes
1 answer

How can I get the classname from a static call in an extended PHP class?

Hello @kartik, __CLASS__ always returns the name of the ...READ MORE

answered Oct 27, 2020 in PHP by Niroj
• 82,880 points
765 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,880 points
22,323 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,880 points
2,920 views
0 votes
1 answer

How to enable Bootstrap tooltip on disabled button?

Hii @kartik, You can wrap the disabled button ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,880 points
4,638 views
+1 vote
2 answers

How to set cache false for getJSON in jQuery?

You can't pass any configuration parameters to ...READ MORE

answered Oct 7, 2020 in JQuery by Amit
• 140 points
2,456 views
0 votes
1 answer

How can I handle the warning of file_get_contents() function in PHP?

Hello @kartik, This is fairly simple: if (!$data = ...READ MORE

answered Apr 7, 2020 in PHP by Niroj
• 82,880 points
10,413 views
0 votes
1 answer

How can I connect to a Tor hidden service using CURL in PHP?

Hello @kartik, I use Privoxy and cURL to scrape Tor ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,880 points
4,872 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP