-3

Basically what I'm trying to do is do an if command to check a date returned from database vs the current date (todays date or tomorrows date if i go on the page tomorrow)

I was thinking of something along the lines of

$date = "2013-11-10 20:28:41";   
if ($date < Curdate) 
{
    echo "expired";
}

if anyone can give me any insight on the correct commands for this, I would really appreciate it.

John Conde
  • 217,595
  • 99
  • 455
  • 496
user2905326
  • 50
  • 1
  • 7

2 Answers2

1
$dt = new DateTime($date);
$now = new DateTime();
if ($dt < $now) {
  echo "before";
}
else {
  echo "after";
}
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Its a great feature, and im sure ill find some use for it in the future. Never even thought to run a before and after. However with what im currently coding I only need the one command :) Thank you for your time tho – user2905326 Nov 21 '13 at 19:23
0
$now = new DateTime(); // Now
$date = new DateTime('2013-11-10 20:28:41'); // From the DB
if ($date < $now) 
{
    echo "expired";
}

Reference: DateTime()

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Is this not a duplicate? – Amal Murali Nov 21 '13 at 19:13
  • It is. I have voted to close. Any reason you haven't yet? – John Conde Nov 21 '13 at 19:14
  • Why answer the question if you know it's a duplicate? (feel free to, but I was just wondering) :) – Amal Murali Nov 21 '13 at 19:17
  • I didn't until I saw your comment. I always close when a dupe. – John Conde Nov 21 '13 at 19:18
  • Thank you for this, just what i was looking for :) Nice and simply put for a newbie like myself :) – user2905326 Nov 21 '13 at 19:21
  • Oh and keep in mind, it may be a duplicate, but newbies like me don't really know what to search to find the commands we want, or we look at a piece of code that is done so expertly that it can be difficult to decode what we need. - I did spend an hour searching and trying things before i resorted to posting - So yeh thank you for your replys – user2905326 Nov 21 '13 at 19:28