227

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?

Salman A
  • 229,425
  • 77
  • 398
  • 489
alexus
  • 6,278
  • 8
  • 39
  • 59

13 Answers13

299
strtotime($var);

Turns it into a time value

time() - strtotime($var);

Gives you the seconds since $var

if((time()-(60*60*24)) < strtotime($var))

Will check if $var has been within the last day.

Tyler Carter
  • 57,335
  • 20
  • 122
  • 148
219

That format is perfectly appropriate for a standard string comparison e.g.

if ($date1 > $date2){
  //Action
}

To get today's date in that format, simply use: date("Y-m-d H:i:s").

So:

$today = date("Y-m-d H:i:s");
$date = "2010-01-21 00:00:00";

if ($date < $today) {}

That's the beauty of that format: it orders nicely. Of course, that may be less efficient, depending on your exact circumstances, but it might also be a whole lot more convenient and lead to more maintainable code - we'd need to know more to truly make that judgement call.

For the correct timezone, you can use, for example,

date_default_timezone_set('America/New_York');

Click here to refer to the available PHP Timezones.

yanko
  • 44
  • 8
Bobby Jack
  • 14,812
  • 10
  • 59
  • 94
  • 2
    This also ignores timezone – anushr Sep 04 '15 at 11:48
  • 1
    @anushr, if timezone needed in comparison, time difference can be added to date before compare. This is still faster than trying to convert two date time strings in `mm/dd/yyyy` format to date and then comparing it. It always worry me when developers have dates in multiple time zone in their system. I think it is better to store dates in your system, always in single time zone and then display it in whatever timezone required. I started using `yyyy-mm-dd` format (or `yyyymmdd`) around 1998 when I realized it sorts a lot faster even in SQL server. – AaA Jan 29 '20 at 07:39
  • I've used something like this previously, but having this answer easily accessible really helps in a pinch. Not sure why they chose the second-based one as the "best" when this one is literal dates! – Charles John Thompson III Oct 13 '20 at 05:18
  • This worked fine for me, and to those who struggle on where to put the timezone, you can include the code `date_default_timezone_set('America/New_York');` anywhere on your PHP file. To only get the date use the code `date("Y-m-d")` – chanaka wickramasinghe Mar 16 '21 at 17:53
61

Here you go:

function isToday($time) // midnight second
{
    return (strtotime($time) === strtotime('today'));
}

isToday('2010-01-22 00:00:00.0'); // true

Also, some more helper functions:

function isPast($time)
{
    return (strtotime($time) < time());
}

function isFuture($time)
{
    return (strtotime($time) > time());
}
Alix Axel
  • 141,486
  • 84
  • 375
  • 483
  • Why the weird backwards logic and extraneous booleans? Why not just "return strtotime($time) == strtotime('today')", "return strtotime($time) > time()", and "return strtotime($time) < time()"? – Bobby Jack Jan 22 '10 at 00:20
  • @Bobby Jack: I had more functionality in those functions, forgot to delete the extra code. Fixed now. – Alix Axel Jan 22 '10 at 00:29
  • +1 on the helper function, specifically isPast b/c I just ran a `if(strtotime('2013-01-22 00:00:00.00') < time()){....}` and that did it for me. – MDMoore313 Jan 23 '13 at 15:14
48

You can use the DateTime class:

$past   = new DateTime("2010-01-01 00:00:00");
$now    = new DateTime();
$future = new DateTime("2021-01-01 00:00:00");

Comparison operators work*:

var_dump($past   < $now);         // bool(true)
var_dump($future < $now);         // bool(false)

var_dump($now == $past);          // bool(false)
var_dump($now == new DateTime()); // bool(true)
var_dump($now == $future);        // bool(false)

var_dump($past   > $now);         // bool(false)
var_dump($future > $now);         // bool(true)

It is also possible to grab the timestamp values from DateTime objects and compare them:

var_dump($past  ->getTimestamp());                        // int(1262286000)
var_dump($now   ->getTimestamp());                        // int(1431686228)
var_dump($future->getTimestamp());                        // int(1577818800)
var_dump($past  ->getTimestamp() < $now->getTimestamp()); // bool(true)
var_dump($future->getTimestamp() > $now->getTimestamp()); // bool(true)

* Note that === returns false when comparing two different DateTime objects even when they represent the same date.

Salman A
  • 229,425
  • 77
  • 398
  • 489
47

To complete BoBby Jack, the use of DateTime OBject, if you have php 5.2.2+ :

if(new DateTime() > new DateTime($var)){
    // $var is before today so use it

}
timrau
  • 21,494
  • 4
  • 47
  • 62
kamakazuu
  • 471
  • 4
  • 2
  • This is not correct. If $var is current day (after exact midnight), new DateTime() will be bigger then new DateTime($var) and it's not true $var is before today. – dxvargas Jan 15 '20 at 10:20
13
$toBeComparedDate = '2014-08-12';
$today = (new DateTime())->format('Y-m-d'); //use format whatever you are using
$expiry = (new DateTime($toBeComparedDate))->format('Y-m-d');

var_dump(strtotime($today) > strtotime($expiry)); //false or true
Almo
  • 14,789
  • 13
  • 64
  • 91
Hassan
  • 155
  • 1
  • 4
6

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

omega
  • 341
  • 1
  • 2
  • 15
Nelu Bidonelu
  • 185
  • 2
  • 4
6

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,

//  input
$var = "2016-11-09 00:00:00.0";

//  check if date is today or in the future
if ( time() <= strtotime($var) ) 
{
    //  This seems right, but if it's ONLY date you are after
    //  then the code might treat $var as past depending on
    //  the time.
}

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:

//  plain date string
$input = "2016-11-09";

Because the input is plain date string, using strtotime() on $input will assume that it's the midnight of 2016-11-09. So, running time() 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:

if (date("Y-m-d") <= $input)
{
    echo "Input date is equal to or greater than today.";
}
Lynnell Neri
  • 463
  • 1
  • 10
  • 16
  • 1
    I'm sure OP considered this and that is why they included `00:00:00.0' in their question – AaA Jan 29 '20 at 07:45
5
$date1=date_create("2014-07-02");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);

(the w3schools example, it works perfect)

Josua Marcel C
  • 4,264
  • 5
  • 43
  • 83
Richard de Ree
  • 1,561
  • 2
  • 21
  • 40
4

Expanding on Josua's answer from w3schools:

//create objects for the dates to compare
$date1=date_create($someDate);
$date2=date_create(date("Y-m-d"));
$diff=date_diff($date1,$date2);
//now convert the $diff object to type integer
$intDiff = $diff->format("%R%a");
$intDiff = intval($intDiff);
//now compare the two dates
if ($intDiff > 0)  {echo '$date1 is in the past';}
else {echo 'date1 is today or in the future';}

I hope this helps. My first post on stackoverflow!

enceladus
  • 71
  • 4
2

Some given answers don't have in consideration the current day!

Here it is my proposal.

$var = "2010-01-21 00:00:00.0"
$given_date = new \DateTime($var);

if ($given_date == new \DateTime('today')) {
  //today
}

if ($given_date < new \DateTime('today')) {
  //past
}

if ($given_date > new \DateTime('today')) {
  //future
}
dxvargas
  • 768
  • 13
  • 22
1

Compare date time objects:

(I picked 10 days - Anything older than 10 days is "OLD", else "NEW")

$now   = new DateTime();
$diff=date_diff($yourdate,$now);
$diff_days = $diff->format("%a");
if($diff_days > 10){
    echo "OLD! " . $yourdate->format('m/d/Y');
}else{
    echo "NEW! " . $yourdate->format('m/d/Y');
}
Kevin
  • 1,972
  • 15
  • 18
0

If you do things with time and dates Carbon is you best friend;

Install the package then:

$theDay = Carbon::make("2010-01-21 00:00:00.0");

$theDay->isToday();
$theDay->isPast();
$theDay->isFuture();
if($theDay->lt(Carbon::today()) || $theDay->gt(Carbon::today()))

lt = less than, gt = greater than

As in the question:

$theDay->gt(Carbon::today()) ? true : false;

and much more;