129

I'm trying to write a script that will check if the current date/time is past the 05/15/2010 at 4PM

How can I use PHP's date() function to perform this check?

Salman A
  • 262,204
  • 82
  • 430
  • 521
dave
  • 7,717
  • 19
  • 68
  • 100

4 Answers4

249

Since PHP >= 5.2.2 you can use the DateTime class as such:

if (new DateTime() > new DateTime("2010-05-15 16:00:00")) {
    # current time is greater than 2010-05-15 16:00:00
    # in other words, 2010-05-15 16:00:00 has passed
}

The string passed to the DateTime constructor is parsed according to these rules.


Note that it is also possible to use time and strtotime functions. See original answer.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • 6
    timestamp has some limitations mate, and some bugs too, for example a timestamp can't handle a date before 1970 and after 2038 (see Ivar Koster's answer) – Steel Brain Aug 02 '14 at 08:52
  • 1
    We often see an abuse of new DateTime() with no parameter, if you are using UTC dates (as you should), prefer to use new DateTime("now", new DateTimeZone('UTC')). This will prevent your app from misunderstandable bugs in dates. i advise you to store it in a function static variable, a class static or somewhere else in your running process... – Loenix Oct 12 '16 at 08:01
  • What if we have already a DateTime object and we want to know if this is past as we cannot pass an object to DateTime constructor as you suggested? should we use its getTimestamp() and compare it with time()? @Salman A – user4271704 Oct 11 '19 at 20:14
  • 1
    @user4271704 The `new DateTime('2010-05-15')` is just an example, could be any DateTime object that you initialized earlier. – Salman A Oct 11 '19 at 21:02
  • assuming `$object` is a DateTime object, I used `if (new DateTime() > $object) {}` and it works wrongly. But `if (time() > $bject->getTimeStamp()) {}` works correcly. @Salman A – user4271704 Oct 11 '19 at 21:21
  • As I said above, your answer is wrong. objects cannot be compared with `<=>`. @SalmanA @dave – user4271704 Oct 11 '19 at 21:55
  • @user4271704 DateTime objects can be compared with >, < operators since PHP 5.2.2 and it is mentioned in the manual. – Salman A Oct 12 '19 at 11:13
80

There's also the DateTime class which implements a function for comparison operators.

// $now = new DateTime();
$dtA = new DateTime('05/14/2010 3:00PM');
$dtB = new DateTime('05/14/2010 4:00PM');

if ( $dtA > $dtB ) {
  echo 'dtA > dtB';
}
else {
  echo 'dtA <= dtB';
}
VolkerK
  • 95,432
  • 20
  • 163
  • 226
6

Check PHP's strtotime-function to convert your set date/time to a timestamp: http://php.net/manual/en/function.strtotime.php

If strtotime can't handle your date/time format correctly ("4:00PM" will probably work but not "at 4PM"), you'll need to use string-functions, e.g. substr to parse/correct your format and retrieve your timestamp through another function, e.g. mktime.

Then compare the resulting timestamp with the current date/time (if ($calulated_timestamp > time()) { /* date in the future */ }) to see whether the set date/time is in the past or the future.

I suggest to read the PHP-doc on date/time-functions and get back here with some of your source-code once you get stuck.

Select0r
  • 12,234
  • 11
  • 45
  • 68
1
date_default_timezone_set('Asia/Kolkata');

$curDateTime = date("Y-m-d H:i:s");
$myDate = date("Y-m-d H:i:s", strtotime("2018-06-26 16:15:33"));
if($myDate < $curDateTime){
    echo "active";exit;
}else{
    echo "inactive";exit;
}