Date - is today

I am making a forum. I want to be able to tell if a date for any given post was made today… or made later. So if the thread was started today then I would post a time and if it was made before today then I would put a date. How could I test if a date is today.

How are you storing the dates? Datetime?

if (date('Y-m-d') == substr($post['date'],0,10)) {
  //It was today.
} else {
 //It wasnt.
}

10: XXXX-YY-ZZ = 10 characters.

In MySQL you could use CURDATE()

That would greatly depend on what format your date is in.

The dates are stored in datetime.

Thanks anthony

I shall try that

Still a bit unsure about this

I want to compare todays date to a variable from a database which I write as $row[1]

How would I do that

You can either as the database for an extra field to indicate if the post was created “today”, or you can use StarLion’s suggestion.

It’s about control/preference really.


<?php
function is_today($datetime){
  return date('Y-m-d') === substr($datetime, 0, 10);
}

var_dump(
  is_today('2011-06-24')
); #bool(true)

var_dump(
  is_today('2011-06-24 00:00:00')
); #bool(true)

var_dump(
  is_today('')
); #bool(false)

var_dump(
  is_today('2011-06-25 00:00:00')
); #bool(false)

Thanks

That worked.