I have following
$var = "2010-01-21 00:00:00.0"
I'd like to compare this date against today's date (i.e. I'd like to know if this $var
is before today or equals today or not)
What function would I need to use?
I have following
$var = "2010-01-21 00:00:00.0"
I'd like to compare this date against today's date (i.e. I'd like to know if this $var
is before today or equals today or not)
What function would I need to use?
Here you go:
Also, some more helper functions:
Expanding on Josua's answer from w3schools:
I hope this helps. My first post on stackoverflow!
One caution based on my experience, if your purpose only involves date then be careful to include the timestamp. For example, say today is
"2016-11-09"
. Comparison involving timestamp will nullify the logic here. Example,The code above seems right, but if it's ONLY the date you want to compare, then, the above code is not the right logic. Why? Because, time() and strtotime() will provide include timestamp. That is, even though both dates fall on the same day, but difference in time will matter. Consider the example below:
Because the input is plain date string, using
strtotime()
on$input
will assume that it's the midnight of 2016-11-09. So, runningtime()
anytime after midnight will always treat$input
as past, even though they are on the same day.To fix this, you can simply code, like this:
To complete BoBby Jack, the use of DateTime OBject, if you have php 5.2.2+ :
You can use the
DateTime
class:Comparison operators work*:
It is also possible to grab the timestamp values from DateTime objects and compare them:
* Note that
===
returns false when comparing two different DateTime objects even when they represent the same date.