Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Joe Bruno
Joe Bruno
35,909 Points

PHP for loop, Stripe, and Unix Time Stamp

Hello,

I have a php for loop which is correctly outputting the desired data from the Stripe API; however, Stripe formats their time stamps via the Unix Timestamp method. I am trying to convert those something more user friendly. I am able to do so outside of the loop, but inside, I receive a fatal error - even though the correct output IS displayed on my screen. Help?

    <?php if ( is_user_logged_in() ) {

        $customer_id = get_user_meta(   
           get_current_user_id(), '_stripe_customer_id', true ); 

                if ($customer_id) {

                      $customer =  Stripe_Customer::retrieve( $customer_id );

                    for($i = 0; $i < $customer_subscriptions_total_count; $i ++) {

                            $customer_subscriptions_data = $customer_subscriptions_array[$i];
           $subscription_id =  $customer_subscriptions_data->id; 
                            $subscription_start = $customer_subscriptions_data->start;

                            //attempting to return formatted unix timestamp, which I am trying to convert to m/d/Y
                                $unixTime = DateTime::createFromFormat('U', $subscription_start);
                                $subscription_start_formatted = $unixTime->format('m/d/Y');

                            //Interestingly it "works" beacuse the correct output is displayed on the screen, but then:
                            // Fatal Error: Call to a member function format() on a non-object

                            $subscription_status = $customer_subscriptions_data->status; 
                            $subscription_customer = $customer_subscriptions_data->customer;
                            $subscription_current_period_start = $customer_subscriptions_data->current_period_start;  
                            $subscription_current_period_end = $customer_subscriptions_data->current_period_end;  

                            $subscription_interval = $customer_subscriptions_data->plan->interval;
                            $subscription_name =  $customer_subscriptions_data->plan->name;
                            $subscription_created = $customer_subscriptions_data->plan->created;
                            $subscription_amount = $customer_subscriptions_data->plan->amount;
                            $subscription_id = $customer_subscriptions_data->plan->id;
                            $subscription_interval_count = $customer_subscriptions_data->plan->interval_count;
                            $subscription_statement_description = $customer_subscriptions_data->plan->statement_description;





                            echo "<div class='row container' style='background:#eee; color: #303030;'>";
                                echo "<div class='col-md-2'>"  . $subscription_start_formatted . "</div>";
                                echo "<div class='col-md-2'>"  . $subscription_status . "</div>";
                                echo "<div class='col-md-2'>"  . $subscription_current_period_start . "-" . $subscription_current_period_end . "</div>";
                                echo "<div class='col-md-2'>"  . $subscription_interval . "</div>";
                                echo "<div class='col-md-2'>"  . $subscription_name . "</div>";
                                echo "<div class='col-md-2'>$"  . $subscription_amount / 100 . ".00</div>";
                            echo "</div>";


                        }



                            //outside of loop works (?)
                          $subscription_start = $customer->subscriptions->data[1]->start;
                                $unixTime = DateTime::createFromFormat('U', $subscription_start);
                                $subscription_start_formatted = $unixTime->format('m/d/Y');
                                echo "<br>FORMATTED TIME" . $subscription_start_formatted . "<br>";

                }
           }

         ?>

2 Answers

echo the unix timestamp prior to doing the Datetime work and see what the value is before it goes to the date time, there may be a bad timestamp being passed from Stripe. That would most certainly give you a fatal error.

Joe Bruno
Joe Bruno
35,909 Points

James,

You are correct. The issue was a couple of bad timestamps in the data from subscription plans I tried to create and (partially) created earlier in the development and testing process. Changing the parameters of the for loop to

  for($i = 0; $i < 10; $i ++) or dynamically to   for($i = 0; $i < count($customer_subscriptions_array); $i ++)

solves the issue. It seems that Stripe's total_count parameter includes attempted or partially created subscriptions. Thank you for the help!

no problem. Bad data will be a constant problem with you're programming endeavors. You need to sanitize anything that is beyond your control. it's a good practice to get into and will save you a lot of headaches.