2

Why I can only reach my date object when I dump() it before using it?

Here is my function:

public function checkSubscriptionEndDate($user){

    $subscriptionEndDate = $user->getSubscriptionEndDate();

    dump($subscriptionEndDate);

    if($subscriptionEndDate==null){
        $subscriptionEndDateMessage = $this->get('translator')->trans('subscriptionEndDateMessage');
        $subscriptionStatus = "error";
    }else{

        $subscriptionEndDateDate = $subscriptionEndDate->date;
        // CHECK IF SUBSCRIPTION END DATE IS BEFORE NOW
        if (date('now') < $subscriptionEndDateDate) {
            dump('before');
            $subscriptionStatus = "success";
            $subscriptionEndDateMessage = $this->get('translator')->trans('subscriptionStatusSuccess').' '.date('d/m/Y',strtotime($subscriptionEndDateDate));
        }else{
            dump('after');
            $subscriptionStatus = "error";
            $subscriptionEndDateMessage = $this->get('translator')->trans('subscriptionStatusError').' '.date('d/m/Y',strtotime($subscriptionEndDateDate));
        }   
    }

    return array(
        'subscriptionEndDateMessage' => $subscriptionEndDateMessage,
        'subscriptionStatus' => $subscriptionStatus
    );

    return $subscriptionEndDateMessage;

}

When I do this, the page loads as expected, there is no problem. But if I remove this line:

dump($subscriptionEndDate);

I can no more load the $subscriptionEndDate->date as illustrated in this error page:

enter image description here

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Kr1
  • 1,269
  • 2
  • 24
  • 56

1 Answers1

7

This looks like another example of this strange-looking thing with var_dump/print_r and DateTime objects that people often ask about.

The date property is added to the DateTime object in $subscriptionEndDate by the dump function, to provide a human readable representation of the internal data, but it isn't a property of the object by default.

Instead of getting the string, you can just create a new DateTime object to represent the current date and time. Those objects are directly comparable (since PHP 5.2.2).

So instead of this:

$subscriptionEndDateDate = $subscriptionEndDate->date;
// CHECK IF SUBSCRIPTION END DATE IS BEFORE NOW
if (date('now') < $subscriptionEndDateDate) {

use this:

$now = new DateTime;
// CHECK IF SUBSCRIPTION END DATE IS BEFORE NOW
if ($now < $subscriptionEndDate) {

If you haven't converted $subscriptionEndDate to the string $subscriptionEndDateDate, this later part won't work:

date('d/m/Y',strtotime($subscriptionEndDateDate)

So you can use the format method instead.

$subscriptionEndDate->format('d/m/Y')

This seems better anyway, in my opinion, because you won't be converting from DateTime to string to timestamp back to string, you'll just go directly to string once.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • 2
    You sir, are a life saver! I had that like 2 years ago too and I couldnt manage it to work lol. The project is dead by now, but please sir, take my upvote. – Manuel Mannhardt Nov 14 '17 at 17:50
  • @Manuel glad I could help? :) If help is even the right word for it at this point... – Don't Panic Nov 14 '17 at 17:54
  • Ill bookmark your answer for sure, maybe you can help my future me :) Its the same thing as die/exit with numbers between 1-255 when debugging, it drove me really mad haha – Manuel Mannhardt Nov 14 '17 at 17:57