Compare given date with today

前端 未结 13 2137
别那么骄傲
别那么骄傲 2020-11-22 16:22

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

13条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 16:43

    Few years later, I second Bobby Jack's observation that last 24 hrs is not today!!! And I am surprised that the answer was so much upvoted...

    To compare if a certain date is less, equal or greater than another, first you need to turn them "down" to beginning of the day. In other words, make sure that you're talking about same 00:00:00 time in both dates. This can be simply and elegantly done as:

    strtotime("today") <=> strtotime($var)
    

    if $var has the time part on 00:00:00 like the OP specified.

    Replace <=> with whatever you need (or keep it like this in php 7)

    Also, obviously, we're talking about same timezone for both. For list of supported TimeZones

提交回复
热议问题