12

I have found hundreds of questions and answers for topics SIMILAR to this on SO, however, none match my needs specifically and I am stumped.

I have a variable in y-m-d format and I need to see if it was created on the previous calendar day (not 24 hours previous, but on the previoous calendar day).

ie. $tDate = '12-05-2';

If object created May 2, 2012 at 11:59pm(stored time) I need a comparison to May 3, 2012 12:01 am(current time) to equal true.

If object created May 2, 2012 at 11:51pm(stored time) I need a comparison to May 2, 2012 11:58pm(current time) to equal false.

I know if these were stored in a MySQL db and pulled from a field, MySQL could figure that out easily. In this case, however, that solution is not an option.

This comparison must be done entirely in php.

I know it's an eccentric question, but hey, that's what the guru's at StackOverflow excel at! Looking forward to seeing the replies!

UPDATE

Figured this out as:

    $dTest = '12-05-02';
    $dTest = explode('-',$dTest);
    $dTest2 = date('y-m-d');
    $dTest2 = explode('-',$dTest2);

    if ($dTest[2]<$dTest2[2]){
        echo '<br />Posted Yesterday<br />';
    } else {
        echo '<br />Posted Today<br />';
    }

Is there a more efficient solution? Seems to work, but I figure there must be a more optimal/elegant solution?

SOLVED

$tHolder = '12-05-12';
$voteDate = date("y-m-d", strtotime($tHolder));
$today = date("y-m-d", strtotime("today"));

if ($voteDate === $today)
{
   echo "this was today's post";
}
elseif ($voteDate < $today)
{
   echo "this was previous to today";
}
MaurerPower
  • 2,046
  • 7
  • 26
  • 48
  • Figured this out as: $dTest = '12-05-02'; $dTest = explode('-',$dTest); $dTest2 = date('y-m-d'); $dTest2 = explode('-',$dTest2); if ($dTest[2]<$dTest2[2]){ echo '
    Posted Yesterday
    '; } else { echo '
    Posted Today
    '; } Is there a more elegant way to do this?
    – MaurerPower May 03 '12 at 05:59
  • Gah, code formatting does not work in comments! – MaurerPower May 03 '12 at 06:01
  • I will be testing these answers today, and select the best answer. Upon scanning the code snippets every sent, they all do a good job of comparing dates the way I am looking for. Again, will accept/post my results soon! – MaurerPower May 05 '12 at 19:41

3 Answers3

9

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":
}
Laurence
  • 58,936
  • 21
  • 171
  • 212
4

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;
}
s.webbandit
  • 16,332
  • 16
  • 58
  • 82
3

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 />";
}
TheCheese
  • 361
  • 1
  • 3
  • 7