php comparing dates for day change
Firstly - I dont think your "solutions" works. What happens when todays date is 12-06-01 and the post was on 12-05-31 - it will give the wrong answer because "31" > "1"
Anyway - I think the correct answer is:
$yesterday =date("y-m-d", strtotime("yesterday"));$today = date("y-m-d", strtotime("today"));if ($yesterday === $tDate){ echo "this was yesterdays post";}elseif ($today === $tDate){ echo "this was todays post";}else{ echo "this was NOT yesterday or today":}
If you're actually looking for "Posted X days ago":
$datetime1 = new DateTime('2012-05-01');$datetime2 = new DateTime('2012-05-02');$interval = (int)$datetime1->diff($datetime2)->format('%a');switch ($interval) { case 0: echo "Posted Today<br />"; break; case 1: echo "Posted $interval day ago<br />"; break; default: echo "Posted $interval days ago<br />";}
You can convert both dates to UNIX time and then compare it as integers:
$day_start = strtotime('-1 day', mktime(0, 0, 0);$day_finish = strtotime('-1 day', mktime(23, 59, 59);$dT = strtotime('-1 day', $dTime)if($dT > $day_start && $dT < $day_finish) { var_dump($dT);} else { exit;}