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
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" ; ?>
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" );
The date_sub() function subtracts some days, months, years, hours, minutes, and seconds from a date.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With