When I need to implement some calculation, I always keep in mind what is it that I need. For example, if I need to add some seconds to a datetime, I need a datetime somewhere in the future. That is, I need a class called Future
. How is it identified? What data does it need to operate? It seems that there should be at least two values: a datetime relative to which I need a future date, and an interval which defines a time distance between start datetime and desired datetime.
So here is a code:
use Meringue\ISO8601DateTime\FromISO8601 as DateTimeFromISO8601String;
$f =
new Future(
new DateTimeFromISO8601String('2011-01-04T07:59:59+00'),
new NSeconds(674165)
);
Then you can output it in ISO8601 format:
$f->value(); // returns 2011-01-12T03:16:04+00:00
If your initial datetime is not in ISO8601 format, which is the case in your example, you should create a datetime from a custom format, hence a name of the class -- FromCustomFormat
. Since it is a datetime which represents itself in ISO8601 format, it extends an abstract class called ISO8601DateTime
. Here is a complete example:
(new Future(
new FromCustomFormat(
'D M j H:i:s Y',
'Tue Jan 4 07:59:59 2011'
),
new NSeconds(674165)
))
->value();
Here is some more about this approach.