Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php date less than another date

given this kind of date object date("m/d/Y", strtotime($numerical." ".$day." of ".date("F")))

where it may give a mm/dd/yyyy day that is the "first Monday of August", for instance

how can I decide if this date is greater than, less than, or equal to today's date?

I need a now() method that works in this format and can be compared between date objects

I haven't tried date() < date() , yet. But I don't think that will work

any insight appreciated

like image 453
CQM Avatar asked Jun 26 '13 19:06

CQM


People also ask

How do you check if a date is less than another date in PHP?

Comparing two dates in PHP is simple when both the dates are in the same format but the problem arises when both dates are in a different format. Method 1: If the given dates are in the same format then use a simple comparison operator to compare the dates. echo "$date1 is older than $date2" ; ?>

How can I compare two dates in PHP?

Once you have created your DateTime objects, you can also call the diff() method on one object and pass it the other date in order to calculate the difference between the dates. This will give you back a DateInterval object. $last = new DateTime( "25 Dec 2020" ); $now = new DateTime( "now" );

Can I subtract dates in PHP?

The date_sub() function subtracts some days, months, years, hours, minutes, and seconds from a date.


1 Answers

Compare the timestamps:

if (strtotime($numerical." ".$day." of ".date("F")) < time()) {
    // older
} else {
    // newer
}

This is possible as strtotime() returns the seconds since 1.1.1970 and time() too. And in PHP you can easily compare integers...

like image 132
bwoebi Avatar answered Oct 17 '22 19:10

bwoebi