PHP DateTime :: format('U')从现在开始返回时间戳,而不是从指定日期返回?

PHP DateTime :: format('U')从现在开始返回时间戳,而不是从指定日期返回?

问题描述:

I have a DateTime object, initialized with a specific date:

$inputString = '06102013';
$myDate = DateTime::createFromFormat('mdY', $inputString);

Now I want to give it out in different Formats, using

$myFormat = 'd.m.Y';
$myDate->format($myFormat);

This works perfectly fine for all formats up until the point where I say:

$myFormat = 'U';

Then $myDate->format('U'); contains just the current timestamp (the actual time when the function is called). But how do I get the timestamp from the original input date, $inputString in my case?

If this is important: I'm using PHP 5.3, but plan on upgrading soon.

Edit: Forgot to mention that

$myDate->getTimestamp();

also returns the time of calling, not the original time that the DateTime object was created with!

The hours, minutes, seconds part of the timestamp will refer to the current time, yes, because your input timestamp does not contain this information. PHP fills in the blanks from the current time at the point of instantiation. However, the day, month and year part of the timestamp represent your input in any version of PHP I tested (see http://3v4l.org/2Or6T).