3

If I un comment print_r($dateTime) then this statement working fine echo $dateTime->date;

Why this is happening . Give me suggestions please.

 $current_date=date('Y-m-d H:i:s');
 $dateTime = DateTime::createFromFormat("Y-m-d H:i:s", $current_date, (new DateTimeZone('UTC')));

 $dateTime->setTimezone(new DateTimeZone('PST'));
 //print_r($dateTime); //If I print object then below echo stmt is working 

  echo $dateTime->date;

Getting Following Error

Notice: Undefined property: DateTime::$date in C:\xampp\htdocs\datetime.php 

3 Answers3

3

The DateTime class does not have a property called date.

You are probably looking for DateTime::format(string) to output a date with a specific format.
For example:

echo $dateTime->format('Y-m-d H:i')
// prints: 2014-05-13 12:29
Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
  • 1
    Yes !! It's working. But it's giving the object like this DateTime Object ( [date] => 2014-05-13 05:40:09 [timezone_type] => 3 [timezone] => America/Los_Angeles ) –  May 13 '14 at 10:33
  • 2
    @user3580413 - That's impossible. You aren't running Cobra's code at all. – Álvaro González May 13 '14 at 10:34
  • 1
    how to call the DateTime object value date –  May 13 '14 at 10:34
  • It displaying the date with this statement echo $dateTime->format('Y-m-d H:i') @ÁlvaroG.Vicario –  May 13 '14 at 10:39
  • @user3580413 - You don't call any `date` property. I've just composed an answer about that. Cobra's answer is just fine. – Álvaro González May 13 '14 at 10:40
1

Add print_r($dateTime,1); prior to your echo $dateTime->date; and your code will work as expected.

The code..

<?php
$current_date=date('Y-m-d H:i:s');
$dateTime = DateTime::createFromFormat("Y-m-d H:i:s", $current_date, (new DateTimeZone('UTC')));
$dateTime->setTimezone(new DateTimeZone('PST'));
print_r($dateTime,1);  //<-- Add this
echo($dateTime->date); // 2014-05-13 03:35:44 

Demo

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • This is really funny. `print_r()` has the side effect of actually creating a `date` public property. Yet it's a static property, it won't reflect any change to the object (until you call `print_r()` again). – Álvaro González May 13 '14 at 10:46
  • But `$dateTime` is not _static_ in this case right.? – Shankar Narayana Damodaran May 13 '14 at 11:12
  • I mean `$dateTime->date`. If you do `$dateTime->modify('-1234 days')` then `$dateTime->date` keeps its old value (until you call `print_r()` again). It's all clearly a hack of whoever added `print_r()` support to `DateTime` objects and I'm sure not many PHP developers know it. It isn't especially useful but it's a cool finding. – Álvaro González May 13 '14 at 11:17
  • Related: [print_r() adds properties to DateTime objects](http://stackoverflow.com/questions/17066034/print-r-adds-properties-to-datetime-objects) – Álvaro González May 13 '14 at 11:19
1

The public methods and properties of the DateTime class are documented in the PHP manual. As the error message suggests, there's no date property among them. In fact, the class does not have any public property at all, only constants and methods.

As with almost any other object, the only reliable way to cast to string is to use a dedicated method. In DateTime, the method is format().

DateTime is a native object written in C, not a pure PHP class. Thus, print_r() and equivalent functions don't necessarily show the same information as with regular objects.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360